C + + associative containers <map> Simple Summary

Source: Internet
Author: User

C + + associative containers <map> Simple Summary

Map provides variable-size associative containers that efficiently retrieve element values based on associated key Values. When you are working with data on Key-value pairs, you can consider using the Map association Container.

Characteristics:

    • A variable-sized associative container that efficiently retrieves element values based on associated key Values.

    • Reversible because it provides a bidirectional iterator to access its elements.

    • Ordered because its elements are sorted according to the key values of the specified comparison Function.

    • Only. because each of its elements must have a unique key.

    • The associative container pair, because its element data value differs from its key Value.

    • The template class, because it provides functionality that is generic, regardless of the element or key Type. the data type used for elements and keys is specified as a class template and as a parameter in the comparison function and Allocator.

Header Files: #include <map>

Namespaces: using namespace std:map;

Constructor function:

map<key_type, Value_type>map_show//key_type represents a key type, Value_type represents a value type, such as: map<string, int>map_show; means string as key , the associated map of the shaping position

map<int, int>map_int;//similar Arrays

map<string, int>map_str; Similar to string-subscript arrays

Basic Maintenance Operations 1. access

The subscript accesses the "[]" and the at () methods, and the second method is recommended, which checks the Boundary.

For map<string, int>map_str; is accessed in the following ways:

Map_str["firstel") or map_str.at ("firstel");//return key value Firstel corresponding value

int size () const; returns the number of elements in the MAP.

BOOL Empty () const; Determines whether the map is empty, true indicates null, false is Non-null

Iterator Find (const key& Key); returns an iterator in the map that has the corresponding element position of the key equivalent to the specified key.

note: The Count method:map_str.count ("first")// Returns the number of elements in the map whose keys match the key specified by the Parameter. returns 1 if the map contains an element whose sort key matches the parameter key, or 0 if the map does not contain an element with a matching key.

2. add

For map<string, int>map_str, There are several ways to Insert:

Map_str.insert (make_pair ("key", 23)); Insert key value pair {"key", 23} into the map

Map_str.insert ({"key", 23}); Insert key value pair {"key", 23} into the map

Map_str.insert (map_str.begin () +2, make_pair ("key", 23)); Insert {"key" in the second position, 23} key-value pairs

3. Delete

Iterator Erase (const_iterator Where); Remove a key-value pair from where iterator position

int erase (const key_type& key); Remove key value pairs where key values and key are equal

Iterator erase (const_iterator first, const_iterator last); Removes key-value pairs between first and last iterators

void Clear (); clears all key-value pairs within the map element

4. Traverse

It is very convenient to traverse with Iterators.

Iterator begin (); returns an iterator that points to the first element in a map

Iterator End (); Returns an iterator that points to the last element in the map

 for (map<stringint>::iterator map_it = map_str.begin (); map_it! = map_str.end (); map_it++) {      " key:   " " value:    " << map_it->second;   
Map_it->first represents the first value of a key-value pair, which is the value of the key, Map_it->second represents the second value of a key-value pair, which is the value of the key}

member functions
At Finds the element with the specified key Value.
Begin Returns an iterator that points to the first element in the MAP.
Cbegin Returns a const iterator that points to the first element in a map.
Cend Returns a const iterator that exceeds the End.
Clear Clears all the mapped elements.
Count Returns the number of elements in the map whose keys match the key specified in the Parameter.
Crbegin Returns a const iterator that points to the first element in a reverse map.
Crend Returns a const iterator that points to the position after the last element in the reverse map.
Emplace Inserts an element into the map that is constructed on the Ground.
Emplace_hint Inserts the element into the map, with a location hint.
Empty Returns true if the map is Empty.
End Returns an iterator that exceeds the End.
Equal_range Returns a pair of Iterators. The first iterator in this iterator pair points to the first element in a map whose key is greater than the specified key. The second iterator in this iterator pair points to the first element in a map whose key is equal to or greater than the specified key.
Erase Removes the element or range of elements from the map at the specified Location.
Find Returns an iterator that points to the position of the element whose key is equal to the specified key in the MAP.
Get_allocator Returns a copy of the allocator object used to construct the map .
Insert Inserts an element or range of elements into the map at the specified Location.
Key_comp Returns a copy of the comparison object used to sort the keys in the map.
Lower_bound Returns an iterator that points to the first element in the map whose key value is equal to or greater than the key value of the specified key.
Max_size Returns the maximum length of the map.
Rbegin Returns an iterator that points to the first element in the reverse map.
Rend Returns an iterator that points to the position after the last element in the reverse map.
Size Returns the number of elements in the MAP.
Swap Swaps two elements of a map.
Upper_bound Returns an iterator that points to the first element in the map whose key value is greater than the key value of the specified key.
Value_comp Retrieves a copy of the comparison object used to sort the element values in the MAP.
Shrink_to_fit Discard additional Capacity.
Size Returns the number of vector elements
Swap Swaps the elements of two Vectors.

C + + associative containers <map> Simple Summary

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.