Last active
October 27, 2023 07:05
-
-
Save squeeve/60677b4f98ff5fc99c99abb26acf13f9 to your computer and use it in GitHub Desktop.
How to use multimaps with container types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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