Common uses of map in C + + __c++

Source: Internet
Author: User
A detailed explanation of map usage in STL in C + +

Map is an associative container of STL, it provides one-to-one (where the first one can be called a keyword, each keyword can only appear once in a map, the second may be called the value of that keyword), and because of this feature it is possible that when we are dealing with one-to-one data, Provides fast access to programming. Here's the organization of the map's internal data, within the map, a red and black tree is built (a balanced binary tree in a strict sense), which has the ability to automatically sort the data, so all the data within the map is orderly, and we see the orderly benefits behind it.

1. Map Introduction

Map is a kind of associative container. It is characterized by the addition and deletion of the node to the iterator is very small, in addition to that Operation node, the other nodes have no effect.

For iterators, you can modify the real value instead of modifying the key.

2, the function of map

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.

Quickly delete records

Modify the value record according to key.

Traverse all records.

3. Use map

Use map to include the header file where the map class resides

#include <map>/Note that the STL header file does not have an extension. h

The map object is a template class that requires two template parameters for keywords and storage objects:

Std:map<int,string> personnel;

This defines an int as an index and has an associated pointer to a string.

For ease of use, you can do a type definition of the template class,

typedef map<int,cstring> Udt_map_int_cstring;

Udt_map_int_cstring Enummap;

4, the map's constructor

Map altogether provides 6 constructors, which relate to the memory allocator of these things, skip the table, and in the following we'll touch on some of the map's construction methods, and here's what we usually construct a map with the following method:

Map<int, string> mapstudent;

5. Inserting data

After the map container is constructed, we can insert data into it. Here are three ways to insert data:

The first: Insert pair data with the Insert function, the following examples (although the following code is written, should be able to compile in VC and GCC, we can run under what effect, under VC Please add this statement, shielding 4786 warning  #pragma  warning  (disable:4786)  )

  [cpp]  View plain  copy  //Data insertion--first: Insert pair data with insert function    #include  <map>& nbsp;     #include  <string>      #include  <iostream>       using namespace std;      int main ()       {          map<int, string> mapStudent;           mapstudent.insert (pair<int, string> (1, ) Student_one "));          mapstudent.insert (pair<int, string> ( 2,  "Student_two");          mapstudent.insert (pair<int,  String> (3,  "Student_three"));          map<int, string >::iterator iter;          for (iter = mapstudent.begin (); &nbSp;iter != mapstudent.end ();  iter++)               cout<<iter->first<< '   ' <<iter->second<<endl;      }  

Second: Insert value_type data with the Insert function, as illustrated below

  [cpp]  View plain  copy  //second: Insert value_type data with the Insert function, the following is an example of       #include  <map>      #include  <string>      #include   <iostream>      using namespace std;      int main ()       {          map<int, string>  mapstudent;          mapstudent.insert (map<int, string>:: value_type  (1,  "Student_one"));          mapstudent.insert (map <int, string>::value_type  (2,  "Student_two"));           mapstudent.insert (map<int, string>::value_type  (3,  "Student_three"));          map<int, string>::iterator iter;    &nbsP     for (Iter = mapstudent.begin ();  iter != mapstudent.end ();  iter++)              cout<<iter->first< < '   ' <<iter->second<<endl;     }  


The third type: Insert data in an array, as illustrated below

[CPP]

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.