在C++中初始化成对的数组
Initializing an array of pair in C++
我想用以下方式初始化一个成对的数组:
pair<int, int> adjs[4] = {{current_node.first-1, current_node.second}, {current_node.first+1, current_node.second}, {current_node.first, current_node.second-1}, {current_node.first, current_node.second+1}};
然而,我的编译器Code::Blocks 12.1不断抛出错误:
brace-enclosed initializer used to initialize `std::pair<int, int>'|
我以前在一个在线编译器上使用过一次这种方法,它很有效。那么这是编译器的问题还是我代码中的语法问题呢?我不想逐一初始化4对。建议一种我可以消除这个错误的方法。
这个通用初始化语法是C++11的一个特性,您使用的编译器可能不支持C++11,但在线编译器支持。
你可以这样初始化你的数组:
pair<int, int> adjs[4] = {make_pair(current_node.first-1, current_node.second), ...};
一个活生生的例子:http://ideone.com/ggpGX9
相关文章:
- C++使用整数的压缩数组初始化对象
- C++17中函数模板中的静态数组初始化(MSVC 2019)
- 从另一个静态常量数组初始化静态常量数组(只需少量计算)
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 new 和 值进行数组初始化,但没有显式数量的元素
- 运行时C++数组初始化问题
- 使用带有参数包的数组的成员数组初始化类
- 仅通过C++中数组初始化的不同方法,即可在同一输入上获得两个不同的答案
- C++ 2 个指针数组初始化 C2440
- C++结构字符数组初始化
- C++中的多维数组初始化
- constexpr数组初始化
- C++引物动态数组初始化程序的数目超过大小
- 使用std::index_sequence对std::数组初始化进行包扩展
- 当 std 数组初始化太小时,C++会引发错误吗?
- 如何确定结构数组初始化的大小?
- Qt并发错误:数组初始化需要大括号括起来的初始值设定项列表
- 此代码中的数组初始化样式是什么?这是标准的吗?
- C++ 使用数组初始化时的 STL 向量内存管理