mercredi 5 décembre 2018

Insert std::map into std::vector directly

Sorry if the question is very trivial.

I have a vector of maps:

typedef map<char, int> edges;
typedef vector<edges> nodes;

nodes n;

Now let's say I want to push a toy edge. I tried different things and what I worked is

edges e;        //declare an edge
e['c'] = 1;     //initialize it
n.push_back(e);  //push it to the vector

How can I just push the pair of values of an edge ('c' and 2) without having to declare a variable and initialize it?

Something like:

n.push_back(edges('c',2));

but compiler gives an error

error: no matching function for call to ‘std::map<char, int>::map(char, int)’

Aucun commentaire:

Enregistrer un commentaire