我有一张 map 声明如下
map<string, int> symbolTable;
if(tempLine.substr(0,1) == "("){
symbolTable.insert(pair<string, int>(tempLine, lineCount));
}
我如何std::cout我的符号表中的所有内容?
最佳答案
在现代 C++ 中:
for (auto&& item : symbolTable)
cout << item.first << ": " << item.second << '\n';
如果您只能访问 C++11 之前的编译器,代码将是:
for ( map<string, int>::const_iterator it = symbolTable.begin(); it != symbolTable.end(); ++it)
cout << it->first << ": " << it->second << '\n';
关于c++ - std::cout for map<string, int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30678405/