Skip to content

Commit 6e5b962

Browse files
advance_next_prev.cpp
1 parent 2d3034b commit 6e5b962

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

advance_next_prev.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
#include <map>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
map<int, char> mymap = {{1,'a'}, {2,'b'}, {3,'c'},
9+
{4,'d'}, {5,'e'}, {6,'f'}, {7,'g'}, {8,'h'}, {9, 'i'}, {10, 'j'}};
10+
11+
auto it = mymap.begin();
12+
13+
//std::advance change its argument directly and return void
14+
advance(it, 4);
15+
cout << it->first << ", " << it->second << endl; //5, e
16+
17+
advance(it, -2);
18+
cout << it->first << ", " << it->second << endl; //3, c
19+
20+
//std::next doesn't change its argument but make a copy
21+
auto it2 = next(it, 4);
22+
cout << it2->first << ", " << it2->second << endl; //7, g
23+
24+
//std::prev doesn't change its argument but make a copy
25+
auto it3 = prev(it2, 3);
26+
cout << it3->first << ", " << it3->second << endl; //4, d
27+
28+
return 0;
29+
}

0 commit comments

Comments
 (0)