我们定义一个map容器以及它的迭代器itr
std::map<string, int> testMap;
testMap.insert(make_pair<string, int>("A", 1));
testMap.insert(make_pair<string, int>("B", 2));
testMap.insert(make_pair<string, int>("C", 3));
testMap.insert(make_pair<string, int>("D", 4));
testMap.insert(make_pair<string, int>("E", 5));
std::map<string, int>::iterator itr;
输出方式A:
for (itr = testMap.begin(); itr != testMap.end(); ++itr)
{
cout << itr->first << endl;
}
结果:
输出方法B:
for (itr = testMap.begin(); itr != testMap.end(); ++itr)
{
cout << (*itr).first << endl;
}
由此可知(*itr).first 与 itr->first为同一种调用方式
输出方式C:
for (itr = testMap.begin(); itr != testMap.end(); ++itr)
{
cout << &((*itr).first) << endl;
}
此时却输出了一串地址
&(*itr)表示迭代器所指对象地址
itr = testMap.begin();
itr++;
cout << (*itr).first << endl;
const string* ptrTest = NULL;
ptrTest = &((*itr).first);
cout << *ptrTest << endl;
上述代码表示,将迭代器所指对象的地址赋给ptrTest指针