Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fill a map of int and vector<int> in C++?

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.

like image 942
george mano Avatar asked Mar 04 '13 00:03

george mano


People also ask

How do I add a map to a vector file?

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.

Can we store vector in map?

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.

Can we use vector as key in map?

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.


1 Answers

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

like image 200
Daniel Frey Avatar answered Sep 21 '22 10:09

Daniel Frey