c++vectorcontainerspush-back

Push Back in Map


Is there any possibility to use push_back() in maps?

I want to make a map < int, vector<string>> and fill the vector in a loop with strings.

It should look something like this:

map[int] = vector.push_back(string);

Solution

  • If you want to push_back into the vector returned by map[N], just use:

    //assuming
    std::map<int, std::vector<std::string>> my_map;
    int N;
    std::string my_string;
    
    my_map[N].push_back(my_string);