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.
The first element of pair Arr1 is sorted with the pair elements of pair “Arr2”. In the main function, we have initialized the values for pair array “Arr1” and pair array “Arr2”. These sorted arrays and the original pairs array will be displayed by using the cout command.
The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
int arrTwoDim[3][2] = {6, 5, 4, 3, 2, 1}; Example 8 defines a two-dimensional array of 3 sub-arrays with 2 elements each. The array is declared and initialized at the same time. The first element is initialized to 6, the second element to 5, and so on.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With