Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert vector for value in map in C++

Tags:

c++

I am stuck on trying to figure out how to insert a vector for a value in a map. For example:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

int main()

{

    map <int, vector<int> > mymap;   

    mymap.insert(pair<int, vector<int> > (10, #put something here#));

    return 0;
}

I don't know what syntax to use insert a vector for value. I tried {1,2}, but that failed. What syntax should I use?

Everything works if I declare a vector in advance and give it a name, but I don't want to do that, as I want to have a map with a lot of vectors.

Thank You in Advance

like image 571
Akavall Avatar asked May 07 '12 19:05

Akavall


4 Answers

If you want an empty vector you could do:

mymap.insert(pair<int,vector<int> >(10, vector<int>())); 

You could then add whatever elements you want with something like:

mymap[10].push_back(1); mymap[10].push_back(2); 

Edit: Removed incorrect assertion that the vectors would be copied if/when the map grows. As the commenters pointed out, this is not true for std::map, which is node-based.

like image 70
Edward Loper Avatar answered Sep 22 '22 09:09

Edward Loper


Basically your question is not about inserting std::vector into a std::map. Your question is how can you easily create an anonymous std::vector with arbitrary initial element values.

In ISO C++03, you can't. C++11 allows using initialization lists for this, however.

If you are stuck with a C++03 compiler, you possibly could create a helper function to return a vector with specified elements:

std::vector<int> make_vector(int a, int b) {     std::vector<int> v;     v.push_back(a);     v.push_back(b);     return v; } 

If the vectors you're inserting are of different sizes, you could use a variadic function, although doing so would require that you either pass along the number of elements or have a reserved sentinel value.

like image 28
jamesdlin Avatar answered Sep 22 '22 09:09

jamesdlin


If you are using C++11 you can use vector's initialization list constructor (the last constructor in that list) which would look like this:

mymap.insert(pair<int, vector<int> > (10, {1, 2, 3}));

If you can only use C++03, vector has a constructor that takes a size and a default value for each element that might be enough for you. Otherwise you will have to construct the vector and then insert it. If you want to avoid an unnessicary copy of the vector when inserting you could swap it in like so:

vector<int> myvec;
myvec.push_back(1);
myvec.push_back(2);
mymap[10].swap(myvec);

This way the vector won't need to be copied. You'll get an extra vector default construction but that's not very expensive.

like image 35
David Brown Avatar answered Sep 19 '22 09:09

David Brown


#put something here# = vector<int>{1,2}

I'm surprised though that {1,2} didn't work. Are you not using a C++11 compiler? If not then you can only create the vector with default constructor there (no values), or fill it with values first and stick it in.

like image 44
Edward Strange Avatar answered Sep 19 '22 09:09

Edward Strange