Eppok
Eppok

Reputation: 55

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

Upvotes: 2

Views: 6973

Answers (2)

Durgi Rajesh
Durgi Rajesh

Reputation: 1

Map by Default accepts two any same or different datatypes. So, for inserting a vector of strings:

map<int, vector<string>> mp;

for(int i=0;i<n;i++){
    int k; cin>>k;
    string str; cin>>str;
    mp[k].push_back(str);
}

Upvotes: 0

TartanLlama
TartanLlama

Reputation: 65610

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

Upvotes: 3

Related Questions