lundi 5 décembre 2016

Is it possible to assign a vector

You can create a vector by doing:

std::vector<int> v = ({1,2,3});

Suppose you have:

std::unordered_map<int,std::vector<int>> m;

Is there any way to do something like:

m[1] = ({1,2,3}); 

i.e. Is there a way to assign the values in the parenthesis without having to go through the two line step of creating a vector first and then assigning ?

std::vector<int> v = ({1,2,3});
m[1] = v;

Is there anyway to condense the above two lines into a one line step ? Or is the only alternative, to create a function which takes the values that go into the vector and return a vector:

template <class T>
std::vector<T> func2( std::initializer_list<T> list )
{
    std::vector<T> v;
    for( auto elem : list )
    {
        v.push_back(elem) ;
    }
    return v;
}

and then do:

m[1] = func2({1,2,3});

Aucun commentaire:

Enregistrer un commentaire