Skip to content

Instantly share code, notes, and snippets.

@squeeve
Last active October 27, 2023 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squeeve/60677b4f98ff5fc99c99abb26acf13f9 to your computer and use it in GitHub Desktop.
Save squeeve/60677b4f98ff5fc99c99abb26acf13f9 to your computer and use it in GitHub Desktop.
How to use multimaps with container types
#include <iostream>
#include <map>
using namespace std;
typedef vector<int> counts;
void creation(multimap<string,counts>* contents) {
*contents = {
{"a", {1,1,1}},
{"the", {1,1,1}},
{"please", {1,1,1}},
{"people", {1,1,1}},
};
}
int main() {
multimap<string,counts> contents;
multimap<string,counts> emptiedmap;
creation(&contents);
auto f = contents.find("please");
if (f != contents.end()) {
f->second.push_back(30);
cout << "The find 'please' returns ";
for (auto i : f->second)
cout << i << " ";
}
cout << endl;
// meanwhile...
f = emptiedmap.find("anything");
if (f != emptiedmap.end()) {
cout << "Actually found something in emptiedmap." << endl;
} else {
cout << "Was empty, but didn't throw error." << endl;
}
/*
// This throws an error cuz you can't cout a container.
// However, `itr->first` holds the key, and `itr->second` holds the value.
for (auto itr = contents.find("the"); itr != contents.end(); itr++) {
cout << "Found element at " << itr->second << endl;
}
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment