C++:一种在列表中打印所有对的优雅方式
C++:An elegant way to print all pairs in a list
我定义了一个这样的列表:
std::list < pair<string, int> > token_list;
我想打印出所有列表元素,所以我把它写出来:
std::copy(std::begin(token_list),
std::end(token_list),
std::ostream_iterator<pair<string, int> >(std::cout, " "));
但是我一直收到此错误:
error 2679:binary "<<"the operator (or unacceptable conversion) that accepts the right operand oftype (or unacceptable)
在Visual Studio中。我该如何修复它,或者有没有其他方法可以打印出列表中的所有对?
您收到此错误是因为没有std::pair
的重载operator <<
。
但是,打印对列表并不难。我不知道这是否是一个elegant way to print all pairs in a list
但你所需要的只是一个简单的 for 循环:
#include <iostream>
#include <list>
#include <string>
int main() {
std::list<std::pair<std::string, int>> token_list = { {"token0", 0}, {"token1", 1}, {"token2", 2} };
for ( const auto& token : token_list )
std::cout << token.first << ", " << token.second << "n";
return 0;
}
相关文章:
- 以螺旋方式打印矩阵的程序.(工作不好)
- 打印方式:11 21 31
- 打印从SQLite3数据库检索的数据C++与命令行中相同的方式
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- C++|以一种很好的方式将树(不一定是二进制的)打印到stdout
- 阶乘-c++ 我想以不同的方式打印
- 只需要知道我在c ++中打印模式的方式是否有效,或者有另一种方法可以有效地做到这一点
- 在 c++ 中打印到控制台的最佳方式是什么?
- 如何使 printf 以与 NSLog 相同的方式打印字符缓冲区?
- C++:一种在列表中打印所有对的优雅方式
- 如何以这种方式在 C++ 中打印 ASCII 代码值
- 如何以CV_16UC1的方式读取JPG图像并在屏幕上打印?
- Newline更改了C 中打印从文件读取的字符串的方式
- 是否有任何打印2D阵列C 的快捷方式
- C++模板方法选择正确的数据打印方式
- 地图的内容没有以特定的方式打印出来
- C++-创建虚拟函数以打印调试消息的最佳方式
- 为什么我读的所有字符串在打印它们时都以相等的方式结束
- 递归函数以某种方式打印
- 通过 libcups 以编程方式双面打印的正确方法是什么?