Akavall Akavall - 4 months ago 19
C++ Question

Insert vector for value in map in 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
, 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


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