Reputation: 2569
I need a data structure that can hold one to many relationship.
Some thing like a student can attend many courses. only thing is i might have it in order of thousands. one key to multiple values.
map<char,int> mymap;
wont allow to insert same key again.
Upvotes: 1
Views: 5603
Reputation: 361342
I would suggest this:
std::map<Student, std::vector<Course>> StudentInfos;
You just could use student id as key for faster comparison, or you could compare only id
in operator<
when comparing two instances of Student
.
Upvotes: 3
Reputation: 15997
Use std::multimap<Key, T>
and std::multimap<Key, T>::equal_range
if it's okay to duplicate the key a lot. This is probably okay for integers and such.
If you want your key only once, as you will probably want for slightly more complex keys, such as std::string, use std::map<Key, std::vector<T>>
and its find
method.
The nested container is more appropriate for your example. The multimap is really only more appropriate if you have different keys that only "seem" identical respective your predicate.
Upvotes: 1