Simple use of C + + Multimap __c++

Source: Internet
Author: User
Tags strfind

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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.