2

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);
Null
  • 1,950
  • 9
  • 30
  • 33
Eppok
  • 55
  • 1
  • 7

2 Answers2

3

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);
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
0

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);
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83