I am trying to create an multimap of structs , i have declared a struct
struct Student{
Student(){};
Student( string n , int a ){
name = name;
age = age;
}
string name;
int age;
}
created a multimap
multimap< string , Student > classRoom;
and created a function that should push it in the multimap
void addStudent( string name , int age ){
Student tmp( name , age );
classRoom[ name ] = tmp;
}
if i use typical map
this works , but using multimap
this throws
error: no match for ‘operator[]’
Why is this happening and how can i fixt it? Moreover how does the implementation differ in these two?
This has nothing to do with structs; you'd have the same problem with an int
(narrow down your problems!). The problem is that you didn't look up how to use a multimap.
In a map, the []
operator gives you the value that corresponds to a key.
In a multimap, the whole point is that each key may correspond to multiple values, so there cannot be a []
operator.
Use the insert
function instead.
Consult documentation for the language features that you use instead of guessing then giving up!