How can I populate an int and vector <int> map in C ++?

I worked with <map> , where I declared a map as follows:

 map <int, vector<int> > tree; 

I'm trying to assign values ​​to it. My goal is to place multiple values ​​as elements of its keys. Like that:

 0=null 1=>0 2=>1,0 3=>2,1,0 4=>3,2,1,0 5=>0 

I tried to assign the card this way, but it does not work:

 tree[3]=vector<int>(2,1,0); 

However, the following two ways of assigning work are possible:

 tree[1]=vector<int>(0); tree[2]=vector<int>(1,0); 

What is the problem? How can I make a function that works like a Python dictionary?

I do not use C ++ 11.

+5
source share
7 answers

With C ++ 11 you can try:

 tree[3]=vector<int>({2,1,0}); 

In addition, the question may use more detailed information and some code of what you have already tried ...

+6
source

Since you are requesting a C ++ 03 response, this (more verbose than C ++ 11) solution will work.

 tree[3].push_back(2); tree[3].push_back(1); tree[3].push_back(0); 
+6
source

Did you consider std::multi_map ?

 #include <map> int main() { std::multimap<int, int> map; for (int i=1; i < 6; i++) for (int j=1; j < i; j++) map.insert(std::make_pair(i, j)); } 
+1
source

As Daniel Frey points out, you can use

 tree[3] = vector<int>({2,1,0}) 

In pseudocode like python, the constructor vector used here is

 def vector(arr) 

The original post assumes you are trying to use a form constructor

 def vector(*args) 

which does not exist.

If you are not using C ++ 11, consider using one of the other vector constructors .

0
source

Without C ++ 11, the code would not be so graceful:

 tree[0]; // create empty vector for index 0 tree[1].push_back(0); tree[2].push_back(1); tree[2].push_back(0); tree[3].push_back(2); tree[3].push_back(1); tree[3].push_back(0); tree[4].push_back(3); tree[4].push_back(2); tree[4].push_back(1); tree[4].push_back(0); tree[5].push_back(0); 
0
source

I don’t particularly like va_args, but the solution is β€œtidier” to some extent than if you (the user) hadn't messed it up, i.e. types of mixing. Another drawback is that your vector cannot contain -1, but your example does not show it.

 #include <vector> #include <cstdarg> #include <iostream> //Unsafe but it works. template<typename T> std::vector<T> make_vector(T num, ...) { std::vector<T> result; va_list args; va_start(args,num); for(T i = num; i != -1; i = va_arg(args,T)) result.push_back(i); va_end(args); return result; } int main() { std::vector<int> v = make_vector(0,1,2,3,-1); //-1 to stop //do stuff with vector v } 
0
source

Please note that the following two lines do not meet expectations:

 tree[1] = vector<int>(0); tree[2] = vector<int>(1, 0); 

The first parameter of the corresponding vector constructor is the initial container size. The second parameter is the value to initialize the elements of the container. Thus, the first line creates an empty vector, and the second line creates a vector with one element that is initialized to 0.

As pointed out in other answers, push_back() is a good option if you cannot use C ++ 11 functions . However, after upgrading to C ++ 11, you can also initialize the map using nested list initialization as follows:

 int main() { std::map<int, std::vector<int>> tree{ {1, {0}}, {2, {1, 0}}, {3, {2, 1, 0}}, {4, { 3, 2, 1, 0 }}, {5, { 0 }} }; for (auto const &kv : tree) { std::cout << kv.first << " =>"; for (auto const &i : kv.second) std::cout << " " << i; std::cout << std::endl; } return 0; } 

Output:

1 => 0
2 => 1 0
3 => 2 1 0
4 => 3 2 1 0
5 => 0

Ideone Code

0
source

All Articles