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.
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.
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