简体   繁体   中英

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

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.

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.

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.

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

This should work in C++2003 compilers.

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

using namespace std;

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

int main()

{

    map <int, vector<int> > mymap;

    mymap.insert(make_pair(10, make_vector(1,2)));
    // Or, alternatively:
    //   mymap[10] = make_vector(1,2);

    assert(mymap[10][0] == 1);
    assert(mymap[10][1] == 2);

    return 0;
}

C++03 does not have initializer lists, which can be a pain to initialize collections.

If you cannot upgrade to a more modern version of the compiler, you can always use the Boost.Assignment library. It has a list_of function precisely for this.

#put something here# -> boost::assign::list_of(1)(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM