A detailed description of map usage in C + +

Source: Internet
Author: User

Project needs, map of the use of detailed

Key-value pairs match

1 Header Files
#include <map>


2. Function of Map

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
Modify the value record by key.
Traverse all records.


3,

Map constructor

Map provides a total of 6 constructors, this block involves memory allocator these things, skip the table, below we will be exposed to some map construction methods, here is to say, we usually use the following method to construct a map:

Map<int, string> mapstudent;

Defined
Map<string, int> My_map;
or a typedef map<string, int> My_map;
My_map My_map;

4, inserting data
(1) my_map["a"] = 1;

Changing the entries in the map is simple because the map class has overloaded the [] operator

ENUMMAP[1] = "one";

ENUMMAP[2] = "both";

.....

This is very intuitive, but there is a performance issue. Insert 2 o'clock, first find the key 2 in Enummap, not found, and then insert a new object Enummap, the key is 2, the value is an empty string, after the insertion is completed, the string is assigned "two"; This method assigns each value to the default value, which is then assigned to the displayed value, which is more expensive if the element is a class object. We can avoid the overhead in the following ways:

Enummap.insert (Map<int, cstring>:: Value_type (2, "a"))

(2) My_map.insert (map<string, Int>::value_type ("B", 2));
(3) My_map.insert (pair<string,int> ("C", 3));
(4) My_map.insert (make_pair<string,int> ("D", 4));


5.

find and get elements in a map

The subscript operator gives the simplest way to get a value:

CString tmp = enummap[2];

However, it is only true if there is an instance of the key in the map, or an instance is automatically inserted, and the value is the initialization value.

We can use the find () and count () methods to discover whether a key exists.

Find out if the map contains a keyword entry with the Find () method, the parameter passed in is the key to look for, where you need to mention the Begin () and end () two members, representing the first and last entries in the Map object, respectively. The type of these two data is iterator.

int nfindkey = 2; Key to look for

Define an entry variable (actually a pointer)

Udt_map_int_cstring::iterator it= Enummap.find (Nfindkey);

if (it = = Enummap.end ()) {

Didn't find

}

else {

Found it

}


6, iterate data
For (My_itr=my_map.begin ();   My_itr!=my_map.end (); ++MY_ITR) {}

Size of the 7,map

When we insert data into a map, how do we know how much data is currently inserted, and we can use the size function as follows:

Int nSize = Mapstudent.size ();


8, the data is emptied and empty

Emptying the data in the map can use the clear () function to determine if there is data in the map with the empty () function, which returns true to indicate that it is an empty map

9,

If you want to remove 1, use an iterator to remove

Map<int, String>::iterator iter;

iter = Mapstudent.find (1);

Mapstudent.erase (ITER);

If you want to delete 1, delete it with the keyword

Int n = mapstudent.erase (1);//returns 1 if deleted, otherwise 0

Use iterators to delete slices

A little bit of code to empty the map.

Mapstudent.earse (Mapstudent.begin (), Mapstudent.end ());

Delete to be aware of, is also the characteristics of STL, delete interval is a front closed after the set










A detailed description of map usage in C + +

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.