-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_1.html
More file actions
93 lines (78 loc) · 3.28 KB
/
Copy pathCpp2_1.html
File metadata and controls
93 lines (78 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<article>
<h1>C++ II: Page 1 - STL: Vectors</h1>
<section>
<h2>Dynamic Arrays with std::vector</h2>
<p>The <code>std::vector</code> is the workhorse of modern C++ containers. It manages dynamic arrays, automatically handling memory resizing as elements are added or removed.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: vectors.cpp
* Demonstration of std::vector functionality, including reserve and emplace_back
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct Data {
int id;
string name;
};
int main() {
// 1. Creation and Initialization
vector<int> numbers = {10, 20, 30};
// 2. Optimization: reserve()
// Pre-allocating memory avoids costly reallocations
numbers.reserve(10);
// 3. Dynamic resizing: emplace_back()
// More efficient than push_back() for objects (constructs in place)
numbers.push_back(40);
numbers.push_back(50);
// 4. Access
cout << "First element: " << numbers[0] << endl;
cout << "Size: " << numbers.size() << " | Capacity: " << numbers.capacity() << endl;
// 5. Modification
numbers.pop_back(); // Removes 50
// 6. Iteration (Modern C++)
cout << "All elements: ";
for (const auto& n : numbers) {
cout << n << " ";
}
cout << endl;
// 7. Emplacing objects in vector
vector<Data> dataItems;
dataItems.emplace_back(Data{1, "Item1"});
dataItems.emplace_back(1, "Item2"); // Direct construction
// 8. Sorting
sort(numbers.begin(), numbers.end(), greater<int>());
cout << "Sorted (desc): ";
for (int n : numbers) cout << n << " ";
cout << endl;
return 0;
}
/*
* Vector Nuances:
* 1. Vectors automatically manage heap memory.
* 2. 'size()' returns current elements, 'capacity()' returns total space allocated.
* 3. Vectors resize themselves when capacity is exceeded, which can be costly (O(n)).
* 4. Use 'reserve()' if you know the final size to prevent multiple reallocations.
* 5. Accessing out of bounds using '[]' causes undefined behavior; use '.at()' for safety.
* 6. 'emplace_back()' constructs the object in place, avoiding a copy/move operation.
*/
</code></pre>
<div class="interactive-sim">
<h3>Vector Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_1');
out.innerHTML = 'vec = {10, 20};<br>';
setTimeout(() => out.innerHTML += '>> vec.push_back(30);<br>>> Vector size is 3', 800);
">Run Simulation</button>
<div id="sim-output-cpp2_1" class="sim-output"></div>
</div>
</section>
<footer style="margin-top: 40px; padding: 20px; background: #f8f9fa; border-top: 1px solid #dee2e6;">
<div style="display: flex; justify-content: space-between;">
<a href="#" data-file="Cpp1_10.html" style="text-decoration: none; color: #6c757d;">← Previous: RAII & Destructors</a>
<a href="#" data-file="Cpp2_2.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: STL: Maps & Sets →</a>
</div>
</footer>
</article>