Basic use of C + + (12)-map

Source: Internet
Author: User

The standard library map type is a data type that is stored as a key-value (Key-value) .

Map is an associative container for STL. It provides the ability to handle data in one-to-many (where the first can be called a keyword, where each keyword can only appear once in the map, and the second may be called the value of the keyword), and because of this feature, it is possible to provide fast-track programming when we are processing one-to-one data. Here the map internal data organization, map built a red black tree (a non-strict balance of binary tree), the tree has automatic sorting of data functions, so in the map all the data is ordered.

1. Map Features

Map is a type of associative container. It is characterized by the small effect of adding and removing nodes on iterators, except for the Operation node, which has no effect on other nodes. For iterators, you can modify the real value without modifying the key.

Automatically establishes the correspondence of the Key-value. The key and value can be any type you want.

quickly find records based on key values, finding the complexity is basically log (N), if there are 1000 records, find up to 10 times, 1,000,000 records, up to 20 times to find.

Quickly insert Key-value Records. Quickly delete records and modify the value record according to key. Traverse all records.

2. Some basic operations of Map objects

Therefore, if you just look for the existence of the element, you can use the function count(k) , which returns the number of occurrences of k, and if you want to get the value corresponding to the key, you can use the function find(k) , which returns an iterator to that element.

The first: Use the Count function to determine whether the keyword appears, the disadvantage is that the location of the data can not be located, due to map characteristics, one-to-one mapping relationship, it is determined that the count function of the return value of only two, either 0, or 1, the occurrence of the situation, of course, returned 1

The second type: Use the Find function to locate the location of the data, it returns an iterator, when the data appears, it returns the iterator where the data is located, if there is no data to find in the map, it returns an iterator equal to the iterator returned by the End function.

find if a keyword entry is included in the map with find () method, the parameter passed in is the key to look for, where it is necessary to mention the Begin() and End() Two members, representing the first and last entry in the Map object, the type of the two data is iterator.

#include <iostream>#include<algorithm>#include<stdio.h>#include<vector>#include<string>#include<map>using namespacestd;intMain () {map<Char,int>p; //inserting elements in mapP.insert (Make_pair ('a',Ten)); P.insert (Make_pair ('C',9 )); P.insert (Make_pair ('b',Ten )); //when you read an element in a map using the subscript method, it is inserted in the map if the element does not exist in the map. //p[' y '] = 17; //p[' f '] = one;Map<Char,int>:: Iterator it;  for(it = P.begin (); It! = P.end (); it++) cout<< (*it). First <<"   "<< (*it). Second <<Endl; /*use the Count function to determine whether the keyword appears, the disadvantage is that the location of the data can not be located, due to map characteristics, one-to-one mapping relationship, it is determined that the count function of the return value of only two, either 0, or 1, the occurrence of the situation, of course, the return of 1*/    if(P.count ('b'))    {        //returns the number of occurrences, 0 or 1.cout << P.count ('b') <<Endl; }    //use the Find function to locate where the data appears, and it returns an iteratormap<Char,int>:: Iterator iter; ITER= P.find ('b'); if(ITER! =p.end ()) cout<<"Find, the value is"<< (*iter). Second <<Endl; Elsecout<<"Do not Find"<<Endl; System ("Pause"); return 0;}

 Deletes an element method.

#include <iostream>#include<algorithm>#include<stdio.h>#include<vector>#include<string>#include<map>using namespacestd;intMain () {map<Char,int>p; //inserting elements in mapP.insert (Make_pair ('a',Ten)); P.insert (Make_pair ('C',9 )); P.insert (Make_pair ('b',Ten )); //when you read an element in a map using the subscript method, it is inserted in the map if the element does not exist in the map. //p[' y '] = 17; //p[' f '] = one;Map<Char,int>:: Iterator it;  for(it = P.begin (); It! = P.end (); it++) cout<< (*it). First <<"   "<< (*it). Second <<Endl; //If you want to demonstrate the output, choose one of the following, and you'll see better results.//If you want to remove 1, use an iterator to removemap<Char,int>:: Iterator iter; ITER= P.find (1);    P.erase (ITER); //If you want to delete 1, delete it with the keyword    intn = p.erase (1);//Returns 1 if deleted, otherwise returns 0//with iterators, delete the code in pieces and empty the entire map.p.erase (P.begin (), P.end ()); //Delete to be aware of, is also the characteristics of the STL, delete interval is a front closed after the set//from a plus traverse code, print outSystem ("Pause"); return 0;}

Basic use of C + + (12)-map

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.