-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.cpp
73 lines (63 loc) · 1.68 KB
/
iterator_test.cpp
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
#include <iostream>
#include <vector>
#include <map>
using namespace std;
//测试迭代器的定义,其实迭代器就是某个容器内部的一个public的类
class test_iterator
{
public:
class iterator
{
public:
iterator(){};
};
test_iterator(){};
};
void test_const_iterator()
{
cout << "Begin test_const_iterator():\n";
vector<int> vec;
vec.push_back(10);
vec.push_back(12);
vec.push_back(20);
//迭代器的值是常数 即 *it 不能修改
for (vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it)
{
cout << *it << endl;
//*it =100;//错误
}
//常数迭代器
const vector<int>::iterator it = vec.begin();
cout << *it << endl;
//it++; //错误
//迭代器之间加减运算
const vector<int>::iterator it1 = vec.begin();
vector<int>::iterator it2 = vec.end();
size_t newIt = it2 - it1; //相减的类型是int?,通过反汇编发现其实是直接newIt = (it2 - it1) / 4;
cout << "it2 = " << &(*it2) << endl; //0x7fa16040267c
cout << "it1 = " << &(*it1) << endl; //0x7fa160402670
cout << newIt << endl;
}
void test_map_iterator()
{
//map中的iterator 是Forward Iterator
cout << "Begin test_map_iterator()\n";
map<int,int> myMap;
myMap[1] = 100;
myMap[2] = 20;
map<int,int>::iterator it1 = myMap.begin();
//map<int,int>::iterator it2 = it1 + 1; //错误,非线性容器是没有重载+数字的,只重载了++
map<int,int>::iterator it2 = it1++;
--it1;
cout << (*it1).first << " " << (*it1).second << endl;
cout << (*it2).first << " " << (*it2).second << endl;
}
int main()
{
test_iterator a;
test_iterator::iterator itr;
test_const_iterator();
test_map_iterator();
typedef typename vector<int>::iterator iterator1;
return 0;
}