0

当我尝试使用 it.first 打印时,it.second 它不起作用,这些甚至是有效的功能吗?

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
map<string, int> workers;

workers["John"] = 1;
workers["Frank"] = 2;

for(map<string, int>::iterator it = workers.begin(); it != workers.end(); ++it) {
        cout<<it.first()<<":"<<it.second()<<endl;
}

return 0;
}
4

1 回答 1

1

first并且second不是成员函数,它们是普通的成员对象:

cout << it->first << ":" << it->second << endl;

注意没有括号,那些不是函数调用。

于 2013-01-30T03:47:56.307 回答