Map of C + + associative containers insert same key element and find element operation

Source: Internet
Author: User

First, insert the same key element operation (1) Insert method

The keys in the map must be unique, and when you want to insert an element with the same key but different values in the map, no errors occur at compile and run time, and the system ignores the subsequent insertion of the existing key, such as

1 map<int,int> m1;2 M1.insert (Make_pair), 3 M1.insert (Make_pair (1,3)); 4 for (Map<int,int>::iterator Mit=m1.begin (); Mit!=m1.end (); mit++) {5     cout<<mit->first<< ":" <<mit->second<< ENDL;6}


In the second and third rows, where the key is 1, but the value is different, this ignores the insertion of the third row and the result of the program running:

1:2

Inserting using the Insert method is easy to misunderstand:

1. Think the operation of inserting the same key will be error (positive solution: Insert the same key operation, both compile and run without error)

2. When inserting data with the same key, the value of the subsequent insert operation overrides the previous value, for example, the insertion of the third sentence in the example will cause the value of the data corresponding to the final key to be 1 to be modified to 3 (positive solution: The insert operation will be ignored if the key value of the inserted data already exists)

(2) by subscript operation

Inserting an element into a map can also be used to subscript the array (associative array) by looking at the following example

1 map<int,int> m1;2 m1[1]=2;3 m1[1]=3;4 for (Map<int,int>::iterator mit=m1.begin (); Mit!=m1.end (); mit++) {5     cout<<mit->first<< ":" <<mit->second<<endl;6}

Run results

1:3

This code differs from the code in the insert example only in the 2nd, 3 rows, and the assignment operation after the Insert element operation is overridden by the result of the subscript method overrides the preceding assignment operation. The subscript operation will be further analyzed later.

Second, find and read the elements in the map

The biggest advantage of a map container is that it can quickly locate and read the elements of a key value.

(1) Search by subscript

As shown in the following code

1 map<string,int> m1;2 cout<<m1["abc"]<<endl;3 m1["abc"]=8;4 cout<<m1["abc"]<<endl;

Operation Result:

08

As you can see from the code above, we can easily access the elements in the map by means of an array subscript. But you might have some doubts about the output of line 2nd, because initially the map is empty, reading a nonexistent key directly, it actually has the normal output (probably not what we want), and we didn't make any assignment to the key.

Explain the mechanism of subscript index

Using subscript to access an element that does not exist in the map causes the new element pointed to by the subscript to be added to the map whose value is initialized based on the type default or default constructor (such as int or initialize to 0,string).

(2) Search by method count () and find ()

Therefore, if you determine whether a key value element exists in the map, it is generally not possible to judge by the way it is removed, because it will want to add new elements to the map. The Map standard library provides two methods for determining whether an element of a key value exists, the count () method, and the Find () method, as in the following table

Map::count (k) Returns the number of times the key K appears in the map, where only 0 (not present) and 1 (present) can be taken.
Map::find (k) If there is an indexed element in the container that presses K, the iterator that points to that element is returned. Returns an iterator that exceeds the end if it does not exist

These two methods do not insert new elements into the map, so you should use both methods when judging the map index key.

Map of C + + associative containers insert same key element and find element operation

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.