C + + Basics: Map Introduction of C + + standard library

Source: Internet
Author: User
Tags delete key

1. Summary

Map is one of the many container (containers) in the C++stl, a bit like Python's dictionary, map as an associative container that associates key with value, where key is a keyword and is immutable, and value is the relative value of the key value. Map provides a one-to-one data mapping relationship, which in many cases can provide a great deal of programming convenience.

The map interior is implemented by the self-built red-black tree (a balanced binary tree in a strict sense), and the data can be sorted automatically, so all the data inside the map is stored in order. A major feature of map is that the addition and deletion of nodes has little effect on iterators, except for the Operation node, which has no effect on the other nodes.

Map has the following functional features: automatic establishment of key-value correspondence. The key and value can be any type you want. Quickly find records based on key values, find the complexity of the basic log (N), if there are 1000 records, find up to 10 times, 1,000,000 records, up to 20 times. Quickly insert Key-value Records. The quick Delete record modifies the value record according to key. Traverse all records.

2, map of the common operation

First, you need to include the header file #include<map> before using the map, and here's a quick overview of the common actions of the map, which you can see in http://www.cplusplus.com/reference/map/map/.

(1) Constructor:

Map<int,string> Mymap; 
Map<int,string> Mymap2 (Mymap.begin (), Mymap.end ());
In the example, int is the type of key, string is the type of value, and can be other types, such as map<char,float> MYMAP4;


(2) Inserting data: There are several ways, and here are three types of examples.

MYMAP[1] = "one"; Mode 1
mymap.insert (Map<int,string>::value_type (One, "Oneone"));//Mode 2
mymap.insert (Pair<int, String> ("Oneoneone"));/Mode 3
The mode 1 is simple and intuitive, but it is not worth advocating because of its poor performance. When you insert the value= "one" of key = 1, need to first in Mymap, find whether the primary key 1 already exists, if not, then insert a new object into the Mymap, its key is 1, but value is an empty string, after insertion completes, assigns value to "one"; The method first assigns each value to its default value and then assigns it to the displayed value, which is more expensive if the element is a class object. The other way is to effectively avoid this problem.

(3) Find elements:

Using the Find () method, the finding () function returns an iterator that points to the key value, and returns an iterator that points to the tail of the map if it is not found.

Map<int, String>::iterator ITR;
ITR = Mymap.find (1);
if (Itr!=mymap.end ())//If not found, return Mymap.end ()
{
	cout<< "find key 1:" <<endl;
	cout<< itr->first << "" <<itr->second<<endl;//can print the key and value} of the item
else
	cout<< "key is 1 not found. "<<endl;

(4) Delete element:

Implemented using the Erase () method:

	ITR = Mymap.find (1);
	if (Itr!=mymap.end ())
	{
		mymap.erase (ITR);//delete
		//mymap.erase (1) by iterator object;//can also be deleted by primary key
	}

Erase () can also delete a range such as:
Mymap.erase (Mymap.begin (), Mymap.end ());//equivalent to Mymap.clear (), you can clear the contents of the entire map

(5) Swap () method:

Note that the swap () method in the map is for the two map exchange, not for the two elements in the map. Shaped like:

Mymap.swap (MYMAP2)//Exchange two map

(6) Size () method:

Returns the size of the map, that is, the number of elements.

(7) Empty () method

Determines whether the map is empty and returns true if the map is empty.

(8) The Begin () method:

Returns an iterator pointing to the map's head

(9) End () method:

Returns an iterator pointing to the tail of the map

(a) Count () method:

Returns the number of occurrences of the specified element


3. Examples of common operating procedures

Paste a simple assembly run result for better understanding.

Test map usage #include <map> #include <string> #include <iostream> using namespace std;
	void Test_map () {map<int, string > Mymap;//key is int, value is type string map<int, String > Mymap2;
	Pair<int,string> Mypair;
	Mypair.first = 1;
	Mypair.second = "one"; Mymap.insert (Mypair)//Add Data Way 1 Mymap.insert (Map<int,string>::value_type (One, "Oneone"));//Add Data way two mymap2[2] = "Two";
	Add Data method Three, the efficiency is poor, do not advocate mymap2[22] = "Twotwo";
	Mymap2.insert (Pair<int,string> (222, "twotwotwo"));
	Map<int, String>::iterator itr;//iterator cout<< "Mymap content is:" <<endl; for (ITR = Mymap.begin (); Itr!=mymap.end (); itr++)//element traversal {cout<< Itr->first << "" << itr->second&
	lt;<endl;
	The content in cout<< "MYMAP2 is:" <<endl; for (ITR = Mymap2.begin (); Itr!=mymap2.end (); itr++) {cout<< itr->first << "" << itr->second<&
	Lt;endl; The/////element Lookup ITR = mymap.find (11);//find if the element corresponding to a key in the map exists if (itr!=mymap.end ())/If not found, returns MYMAP.End () {cout<< "\ n Find key 11:" <<endl;
		cout<< itr->first << "" <<itr->second<<endl;
		Itr->second = "Oneoneone";
	cout<< "The value of this item is modified to:" <<Mymap[11]<<endl;
	Mymap.swap (MYMAP2);/Note: The Exchange is two map cout<< "\ n after the exchange:" <<endl;
	cout<< "Mymap content is:" <<endl; for (ITR = Mymap.begin (); Itr!=mymap.end (); itr++) {cout<< itr->first << "" << itr->second<&lt
	; Endl;
	The content in cout<< "MYMAP2 is:" <<endl; for (ITR = Mymap2.begin (); Itr!=mymap2.end (); itr++) {cout<< itr->first << "" << itr->second<&
	Lt;endl;
	"\ cout<< Delete key 1 in MYMAP2" <<endl;
	ITR = Mymap2.find (1); if (Itr!=mymap2.end ()) {mymap2.erase (ITR);////mymap2.erase (1)//By key Delete, with the effect of the above is equivalent} cout<< "after the deletion of the MYMAP2 content is:" &
	lt;<endl; for (ITR = Mymap2.begin (); Itr!=mymap2.end (); itr++) {cout<< itr->first << "" << itr->second<&
	Lt;endl; } if (! Mymap.empTy ()) {cout<< "\nmymap size:" <<mymap.size () <<endl;
		Mymap.clear (); Mymap.erase (Mymap.begin (), Mymap.end ())//Is mymap after clear () equivalent cout<< "clear ():" <<mymap.size () <
	<endl;
 }
}



Related Article

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.