c++ - std::cout for map<string, int>

标签 c++ dictionary std cout

我有一张 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/

相关文章:

c++ - 使用变量索引对 vector 的 vector 进行排序

c++ - V8引擎中的隐藏类是如何实现的?

C++ - 使用 g++ 的 Makefile

python - 读取 CSV 文件的每一列作为字典的键

c++ - 我如何定义 map::iterator 列表和 list::iterator 映射

java - 使用自定义等于获取对象的重复计数

c - strtol、strtod 不安全吗?

C++ 如何加速我的程序设计

c++ - 用已知数量的元素填充 vector : specify its size in constructor or by using reserve method?

c++ - 如何有效地在 std::vector 中插入一对?