C + + STL map (map)

Source: Internet
Author: User
Tags month name

header file : #include<map> , of course, the Universal library will not need me to say more.

Description : The map container is a key (key) mapping to a value (value). Because the [] operator is overloaded, the map image is an "advanced version" of the array. The key and value of the map can be any type, where key must define the "less than sign" operator. For example, you can use a map <string,int> month_name map that represents the month name to month number, and then month_name["July"]=7; assign the value in this way.

Statement :

map<key_type,value_type>name;

For example:

map<long long,bool>vis;map<string,int>hash;map<pair<int,int>,vector<int>>test;

In many cases, the map container is used as a hash table, creating a mapping from complex information key (such as a string) to simple information value, such as an integer within a certain range.
Because map is based on a balanced tree implementation, the time complexity of most of its operations is at the \ (O (log n) \) level, slightly slower than the traditional hash table implemented with the hash function. Starting with C++11, the STL has added a hash-based container such as unordered_map, but some algorithmic contests do not support the C++11 standard, and these new containers are no longer described here.

Size/empty/clear/begin/end
Similar to set, respectively, is the number of elements, whether it is empty, empty, first iterator, tail iterator.

Iterators
A map iterator, like set, is also a "bidirectional access iterator." When you dereference the map iterator, you get a two-tuple pair<key_type,value_type> .

Insert/erase
Similar to set, INSERT, delete, respectively. The Insert parameter is pair<key_type,value_type> that the erase parameter can be a pair or an iterator.

map<int,int > h;h.insert(make_pair(1,2)),h.insert(make_pair(2,3));map<int,int > :: iterator it=h.begin();pair<int,int > p=*it;h.erase(it),h.erase(make_pair(2,3));cout<<p.first<<' '<<p.second<<endl;

Find
H.find (x) looks for a two-tuple of key x in a map with the variable named H and returns an iterator to the two-tuple. If it does not exist, return to H.end (). The time complexity is \ (O (log n) \).

[] Operator
H[key] Returns a reference to the value to which the key is mapped, with a time complexity of \ (O (log n) \).
The [] operator is one of the most attractive places to map. We can easily through H[key] to get the value of the key corresponding to the H[key] can also be assigned to change the value of the key corresponding to the value.
It is important to note that if the found key does not exist, then H[key] is created, H automatically creates a new two-tuple (Key,zero) and returns a zero reference. Here zero represents a generalized "0 value", such as an integer 0, an empty string, and so on. If you do not assign a value to H[key] After the lookup, then the time is long, H will contain a lot of useless "0 value two tuples", in vain occupy space, reduce the program running efficiency. It is strongly recommended that readers check the existence of key with the Find method before querying with the [] operator.

[instance] The number of occurrences of a string in map statistics
Given n strings, m questions, each question asks the number of occurrences of a string.
n<=20000,m<=20000, the length of each string is no more than 20.

map<string,int> h;char str[25];for(int i=1;i<=n;i++){    scanf("%s",str);    h[str]++;}for(int i=1;i<=m;i++){    scanf("%s",str);    if(h.find(str)==h.end())puts("0");    else printf("%d\n",h[str]);}

Tips :
The maps in the set and map header files in the Set header file are collections and mappings, respectively. Both support the INSERT, find, count, and remove operations, and you can iterate through the elements in a small-to-large order. Map also provides a "[]" operator so that map can be used like an array. In fact, map is also called "associative array".

C + + STL map (map)

Related Article

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.