【问题标题】:Initializing an array of pair in C++在 C++ 中初始化一对数组
【发布时间】:2014-11-07 22:31:00
【问题描述】:

我想通过以下方式初始化一个pair数组:

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++ compiler-errors stl


    【解决方案1】:

    这种通用初始化语法是 C++11 的一项特性,可能您使用的编译器不支持 C++11,但在线的编译器支持。

    你可以像这样初始化你的数组:

    pair<int, int> adjs[4] = {make_pair(current_node.first-1, current_node.second), ...};
    

    一个活生生的例子:http://ideone.com/ggpGX9

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2023-03-13
      • 2020-12-10
      相关资源
      最近更新 更多