int main()
{
multimap<string, vector<pair<string, unsigned>>> families; // Holds families' names
string cont = "y"; // used to check if user is finished
while (cont == "y" || cont == "Y") { // check if user wants to continue or end
string surename, name; // strings to get surename and children's names
unsigned age = 0;
// get surename
cout << "Enter a family's surename: ";
cin >> surename;
// get childrens name for that family
cout << "Enter childrens names and age seperated by a space, CTRL+D/Z(Linux/Win) to end." << endl;
while (cin >> name >> age) {
auto family = families.find(surename);
if (family != families.end()) {
family->second.push_back({name, age});
} else
families.insert(); // How would I insert that into the multimap?
// statement for std::map : families[surename].push_back({name, age});
}
// check if user wants to end or edit another family
cout << "Would you like to add or edit another family? [Y/n]" << endl;
cin.clear();
cin >> cont;
}
// print families
for(auto it = families.begin(); it != families.end(); ++it) {
cout << it->first << " : ";
for (auto beg = it->second.begin(); beg != it->second.end(); ++beg) {
cout << beg->first << "(" << beg->second << ") ";
}
cout << endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
usingnamespace std;
int main()
{
map<string, vector<pair<string, unsigned>>> families; // Holds families' names
string cont = "y"; // used to check if user is finished
while (cont == "y" || cont == "Y") { // check if user wants to continue or end
string surename, name; // strings to get surename and children's names
unsigned age = 0;
// get surename
cout << "Enter a family's surename: ";
cin >> surename;
// get childrens name for that family
cout << "Enter childrens names and age seperated by a space, CTRL+D/Z(Linux/Win) to end." << endl;;
while (cin >> name >> age) {
families[surename].push_back({name, age});
}
// check if user wants to end or edit another family
cout << "Would you like to add or edit another family? [Y/n]" << endl;
cin.clear();
cin >> cont;
}
// print families
for(auto it = families.begin(); it != families.end(); ++it) {
cout << it->first << " : ";
for (auto beg = it->second.begin(); beg != it->second.end(); ++beg) {
cout << beg->first << "(" << beg->second << ") ";
}
cout << endl;
}
return 0;
}
If your multimap cannot contain 2 elements, you do not need a multimap.
If you still insist, find all entries of key using ::equal_range() member function, and then dereference iterator pointing to one of the found elements and access value (second member in key-value pair)