一个迭代器it,要输出it指向的map的值的方法是cout<<it->second;,今天有个人问我为什么不是it.second。其实,我刚学map的时候就纠结了很久,刚刚才弄明白。map可以看成是一个结构体,it是直接指向整个结构体的,因此(*it).second等价于it->second。
见下面的例子:
struct student
{
int score;
string name;
} a;
int main()
{
cin >> a.name >> a.score;
student *b;
b = &a;
cout << b->name << ' ' << b->score << endl;
cout << (*b).name << ' ' << (*b).score << endl;
cout << a.name << ' ' << a.score << endl;
return 0;
}
/*
Input
hesor 10086
Output
hesor 10086
hesor 10086
hesor 10086
*/