Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ map an integer to an array of boolean

Tags:

c++

I have been trying to create a map from integer to an array of bools. However, the following code does not seem to work.

map<int, bool[]> myMap;
bool one[] = {true, true, false};
myMap[1] = one;

I do not use array that much and there seems to be something seriously wrong here. Can someone point it out? Thanks in advance.

like image 951
Ra1nWarden Avatar asked May 29 '26 10:05

Ra1nWarden


1 Answers

Storing an array like this in a map is not going to work, even if you could do it syntactically: the array is going to stay in the map even after the real array goes out of scope. Storing vectors of bool instead should work:

map<int, vector<bool> > myMap;
vector<bool> one {true, true, false}; // C++11 syntax
myMap[1] = one;
cout << myMap[1][0] << endl;
cout << myMap[1][1] << endl;
cout << myMap[1][2] << endl;

Here is a demo on ideone.

like image 153
Sergey Kalinichenko Avatar answered Jun 01 '26 01:06

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!