-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpp2_3.html
More file actions
84 lines (72 loc) · 3.07 KB
/
Copy pathCpp2_3.html
File metadata and controls
84 lines (72 loc) · 3.07 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
<article>
<h1>C++ II: Page 3 - STL: Iterators</h1>
<section>
<h2>The Bridge to Algorithms</h2>
<p>Iterators are the fundamental mechanism for accessing elements in STL containers. They provide a standardized interface to traverse elements without needing to know the underlying container implementation.</p>
<h3>The Code implementation</h3>
<pre><code class="language-cpp">/*
* File: iterators.cpp
* Demonstration of iterator usage, including reverse iterators and inserters
*/
#include <iostream>
#include <vector>
#include <list>
#include <iterator> // For back_inserter
using namespace std;
int main() {
// 1. Vector Iterator
vector<int> nums = {1, 2, 3};
vector<int>::iterator it = nums.begin();
cout << "First element: " << *it << endl;
// 2. Traversal
cout << "Vector: ";
for (auto i = nums.begin(); i != nums.end(); ++i) {
cout << *i << " ";
}
cout << endl;
// 3. List Iterator
list<string> names = {"Alice", "Bob", "Charlie"};
// 4. Reverse Iterators
cout << "Reverse: ";
for (auto rit = names.rbegin(); rit != names.rend(); ++rit) {
cout << *rit << " ";
}
cout << endl;
// 5. Insert Iterators
vector<int> dest;
copy(nums.begin(), nums.end(), back_inserter(dest));
cout << "Copied vector: ";
for (int n : dest) cout << n << " ";
cout << endl;
return 0;
}
/*
* Iterator Nuances:
* 1. begin(): Points to the first element.
* 2. end(): Points one-past-the-last element (used for termination).
* 3. *it: Dereferences the iterator to access the value.
* 4. ++it: Advances the iterator to the next element.
* 5. rbegin()/rend(): Provide reverse traversal capabilities.
* 6. Iterators can be invalidated if the container is resized (e.g., vector reallocation).
* 7. Insert iterators (back_inserter) simplify the process of adding elements
* to containers via algorithms.
* 8. Const iterators (cbegin(), cend()) ensure read-only access to container elements.
*/
</code></pre>
<div class="interactive-sim">
<h3>Iterator Simulator</h3>
<button class="sim-button" onclick="
const out = document.getElementById('sim-output-cpp2_3');
out.innerHTML = 'it = nums.begin();<br>';
setTimeout(() => out.innerHTML += '>> Value: ' + 1 + '<br>>> ++it;<br>>> Value: ' + 2, 800);
">Run Simulation</button>
<div id="sim-output-cpp2_3" 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="Cpp2_2.html" style="text-decoration: none; color: #6c757d;">← Previous: STL: Maps & Sets</a>
<a href="#" data-file="Cpp2_4.html" style="font-weight: bold; text-decoration: none; color: #007bff;">Next: STL: Algorithms →</a>
</div>
</footer>
</article>