Map insertion method The difference between the subscript method and the Insert method

Source: Internet
Author: User

There are two ways to insert a map in STL:
1. Map <int,int>a;
A[1]=1//This method initializes a[1] and assigns a value to a[1].
a[1]=2//This method modifies the value of a[1.
2 Map <int,int>a;
A.insert (Map<int,int>::value_type ()); This method initializes a[1] and assigns a value to a[1].
A.insert (Map<int,int>::value_type); Because A[1] already exists, this method cannot initialize a[1] and cannot modify the value of a[1].
3 It is particularly important to note that because [] is overloaded to, if the value of the key is not present, the object is created, so the operation is more dangerous.
Map<string,int> Word_count;
int ccurs = word_count["Foobar"];//Key " Foobar"is not in the map container, the subscript operation inserts a new element with that key.


A given key in a map object corresponds to only one element. If the key for the element you are trying to insert is already in the container , insert will not do anything . The version of the insert function that contains one or a pair of iterator parameters does not indicate whether there are or how many elements are inserted into the container.

However, with a key-valuepairof formal parametersInsertThe version returns a value that contains an iterator and aBOOLValue ofpairObject, where the iterator points to theMapelement with the corresponding key in theBOOLThe value indicates whether the element is inserted. If the key is already in the container, its associated value remains unchanged, and the returnedBOOLValue istrue。 In both cases, the iterator will point to the element with the given key. Here's how to useInsertRewrite the word statistics program:

//Count number of times each word occurs in the input
Map<string, int> Word_count; //Empty Map From string to int
string Word;
while (CIN >> word) {
//inserts element with key equal to Word and value1;
//if Word already in Word_count,Insert does nothing
Pair<map<string, int>::iterator, bool> ret =
Word_count.insert (Make_pair (Word, 1));
if (!ret.second)//Word already in Word_count
++ret.first->second; //Increment counter
}

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

Map<string,int> Word_count;
int ccurs = word_count["Foobar"];
However,in themap container, the subscript operation inserts a new element with that key.
In addition, the search element is "[]" returns the reference to the second element in the map (if key exists, no creation of this key exists).
Find () returns an iterator that points to the key.
So, as mentioned above, it is safe to use Insert () and find (), while using "[]" is unsafe.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Map insertion method The difference between the subscript method and the Insert method

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.