using a std::mulimap instead of a std::map

closed account (EwCjE3v7)
I need to use a multimap instead of a map.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
} 


Code that uses a map
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
using namespace 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;
}  
statement for std::map : families[surename].push_back({name, age});
What will you do if there is more than one surename in mmap?
closed account (EwCjE3v7)
well it shouldn't create a new element, it should add the names to the old one as families[surename].push_back({name, age}); does
it should add the names to the old one
Which one of, say, two already existing elements?

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)
closed account (EwCjE3v7)
I know, but it's an exercise from a book I'm learning with and its a little of a stupid one. Thanks I'll try that and get back to you.
Topic archived. No new replies allowed.