gpt4 book ai didi

c++ - 在 C++ 中初始化一对数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:11 25 4
gpt4 key购买 nike

我想用下面的方式初始化一对数组:

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++ 中初始化一对数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26811196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com