-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiterators_in_stl.cpp
More file actions
37 lines (30 loc) · 926 Bytes
/
iterators_in_stl.cpp
File metadata and controls
37 lines (30 loc) · 926 Bytes
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> names{"john", "jane", "jill", "jack"};
vector<string>::iterator it = names.begin(); // or begin(names)
cout << "first name is " << *it << "\n";
++it; // advance the iterator
it->append(string(" goodall"));
cout << "second name is " << *it << "\n";
while (++it != names.end()) {
cout << "another name: " << *it << "\n";
}
// traversing the entire vector backwards
// note global rbegin/rend, note ++ not --
// expand auto here
for (auto ri = rbegin(names); ri != rend(names); ++ri) {
cout << *ri;
if (ri + 1 != rend(names)) // iterator arithmetic
cout << ", ";
}
cout << endl;
// constant iterators
vector<string>::const_reverse_iterator jack = crbegin(names);
cout << "first reverse name is " << *jack << "\n";
// won't work
//*jack += "test";
return 0;
}