I have been working with <map>
, where I declared a map as follows:
map <int, vector<int> > tree;
I am now trying to assign values to it. My goal is to place multiple values as elements of its keys. Something like this:
0=null
1=>0
2=>1,0
3=>2,1,0
4=>3,2,1,0
5=>0
I tried to assign to the map like this, but it does not work:
tree[3]=vector<int>(2,1,0);
However, the following two ways of assigning work:
tree[1]=vector<int>(0);
tree[2]=vector<int>(1,0);
Where is the problem? How can I make a function that works as a Python dictionary?
I am not using C++11.
To convert a map to a vector of key-value pairs, the range should be iterators at the beginning and end of the given map, and the operation should be push_back() to insert each entry into the vector.
Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Map of Vectors in STL: Map of Vectors can be very efficient in designing complex data structures.
In C++ we can use arrays or vector as a key against to a int value like: map<vector<int> ,int > m; Can I do same in MATLAB by containers.
With C++11, you could try:
tree[3]=vector<int>({2,1,0});
Other than that, the question could use more details and some code of what you already tried...
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