std::cout for map<string, int>
std::cout for map<string, int>
我有一个如下声明的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++ 11,这里有一个选择:
for (map<string, int>::iterator it = symbolTable.begin();
it != symbolTable.end(); ++it)
{
cout << it->first << " " << it->second << endl;
}
为完整起见,如果是:
for (auto& s : symbolTable)
{
cout << s.first << " " << s.second << endl;
}
可以使用循环来打印所有的键/值对。下面的代码是c++ 11
中的示例for (const auto& kv : symbolTable) {
std::cout << kv.first << " " << kv.second << 'n';
}
ps:其他两个答案都很少关注const
,这是相当可悲的…
相关文章:
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 两个数组,int 和 string,如何显示带有对应 int 的字符串?
- <string> 如何在使用 SWIG 时将 int 数组和 List 作为参数传递给 C# C++
- OpenCV undefined reference to 'cv::imread(cv::String const&, int)'
- map<string, int> m= {}; 在C++中是什么意思?
- 如何从map<pair<string,int>,pair<string,Array>>中迭代和查找?
- C++将文件行分离为String和Int
- 与 string.size() 比较时 int 的符号性显示警告
- freq_table[field_pm_value] = map<string, int>(); 是什么意思?
- 将 int 附加到 std::string
- 方括号在"map <string, int> instance[numberFeatures];"中是什么意思
- 如何使用单 for 循环遍历 std::map<string>int 和 std::vector<int>?
- 为什么一个简单的bimap(std::string <-> int)不起作用?
- 在 QString 中连接 stl string+int+int+int
- Boost lexical_cast<std::string>(int) MSVC 2008 bug
- 从文件中读取数据类型string, int double.将每一行数据放入对象向量中
- 如何返回包含string/int变量的字符串
- 如何从键盘中读取包含c-string, int和float的结构变量
- 创建string+int set stl类型
- c++ string (int) + string (int)