How to declare, initialize and print a multimap in C++

1 Answer

0 votes
#include <iostream>  
#include <map>
#include <string>

using std::multimap;
using std::string;
using std::cout;
using std::endl;

void printMap(const multimap<string, string>& mp)
{
	for (auto element : mp) {
		cout << element.first << ": " << element.second << endl;
	}
	cout << endl;
}


int main()
{
	multimap<string, string> mm;

	mm.insert({ { "dog", "animal" },
	            { "cat", "animal" },
				{ "programmer", "smart animal" } });

	printMap(mm);

	return 0;
}

/*
run:

cat: animal
dog: animal
programmer: smart animal

*/

 



answered Jan 11, 2018 by avibootz
edited Jan 11, 2018 by avibootz

Related questions

1 answer 201 views
1 answer 196 views
1 answer 188 views
1 answer 100 views
1 answer 136 views
2 answers 230 views
...