Initializing an array of pair in C++

雨燕双飞 提交于 2021-01-27 04:17:15

问题


I want to initialize an array of pair in the following way:

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}};

However my compiler, Code::Blocks 12.1, keeps on throwing the error:

brace-enclosed initializer used to initialize `std::pair<int, int>'|

I used this method once before on an online compiler and it worked. So is it the problem with the compiler or some syntax issue in my code? I don't want to initialize 4 pair one by one. Suggest a way in which I can get rid of this error.


回答1:


This universal initialization syntax is a C++11 feature, likely the compiler you are using does not support C++11 but the online one did.

You can initialize your array like this instead:

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

A live example: http://ideone.com/ggpGX9



来源:https://stackoverflow.com/questions/26811196/initializing-an-array-of-pair-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!