These two days, there is a task to use the C + + Multimap, so wrote a small demo
/** * @File MultiMapDemo.cpp * * @Author: Bob * * @Create time:2016-12-1 11:57:06 * * @Last modify:bob * * @Last Mod Ify time:2016-12-1 11:57:06 * * @Description: * multimap multiple mapping container * multimap multiple mapping container: The data structure of the container is managed by the red-black tree all elements of the Multimap Are pair: The first element is a key value, cannot be modified, the second element is real (value), can be modified * multimap characteristics and usage is exactly the same as map, the only difference is: * Allow elements of duplicate key values to be inserted into the container (using the Rb-tree Insert_ Equal function) * Therefore: * The mapping relationship between the key value key and the element value is a many-to-many relationship * does not define [] operational operations/#include <iostream> #include <string&
Gt
#include <map> int main (int argc, char* argv[]) {std::multimap<std::string, int> mymultimap;
Insert Mymultimap.insert (std::p air<std::string, int> ("Jack", 1));
Mymultimap.insert (std::p air<std::string, int> ("Jack", 2));
Mymultimap.insert (std::p air<std::string, int> ("Bob", 1));
Mymultimap.insert (std::p air<std::string, int> ("Navy", 3));
Mymultimap.insert (std::p air<std::string, int> ("Demo", 4));
Mymultimap.insert (std::p air<std::string, int> ("Bob", 5));
Traverse Std::cout << "================all member" << Std::endl; For (std::multimap<std::string, int>::iterator iter = Mymultimap.begin (); Iter!= mymultimap.end (); ++iter) {STD
:: cout << (*iter). I << ":" << (*iter) second << Std::endl;
//Statistics key is "Jack" number std::string strfind = "Jack";
unsigned int ucount = Mymultimap.count (strfind);
if (Ucount = = 0) {std::cout << "================count" << strfind << ": 0" << Std::endl;
else {std::cout << "================count" << strfind << ":" << ucount << Std::endl;
Std::multimap<std::string, Int>::iterator iter = Mymultimap.find (strfind); if (ITER!= mymultimap.end ()) {for (unsigned int i = 0; i < Ucount; ++i) {std::cout << (*iter). Firs
T << ":" << (*iter). Second << Std::endl;
iter++;
}} std::cout << "================use equal_range" << Std::endl;typedef std::multimap<std::string, Int>::iterator Multimapiterator;
std::p air<multimapiterator, multimapiterator> iterpair = Mymultimap.equal_range ("Jack"); for (Multimapiterator it = iterpair.first it!= iterpair.second ++it) {std::cout << (*it).
lt;< (*it). Second << Std::endl;
//Delete all key "Bob" value pairs Mymultimap.erase ("Bob");
Std::cout << "================after erase Bob" << Std::endl; For (std::multimap<std::string, int>::iterator iter = Mymultimap.begin (); Iter!= mymultimap.end (); ++iter) {STD
:: cout << (*iter). I << ":" << (*iter) second << Std::endl;
//delete duplicate key multimapiterator iter = Mymultimap.find ("Jack");
Mymultimap.erase (ITER);
Std::cout << "================use unique key, erase \ Jack\" "<< Std::endl; For (std::multimap<std::string, int>::iterator iter = Mymultimap.begin (); Iter!= mymultimap.end (); ++iter) {STD :: cout << (*iter). << ":" << (*iter). Second << Std::endl;
return 0; }