C++ std::map

template: 

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;

 

definition:

map specified by sequentially to storage elements and key element value binding.

In the map, it is often used to sort key, and each key is unique in the map. And mapping the type of key values ​​may be different, and the packet type value_type members, this pair is a combination of the two types:

typedef pair<const Key, T> value_type;

Element in the interior, the map is always sorted in accordance with certain stringent conditions as weak ordering in accordance with an internal key comparison target (types Compare) indicated.

map container is typically single element access speed slower than the container unordered_map by their key, they allow direct iteration the subset according to their order.

In the map can be directly accessed using parentheses operator ((operator []) by a corresponding key.

map typically implemented as a binary search tree.

 

Attributes:

Related Associated container elements referenced by their key, they are not referenced by the absolute position in the container.
Sequence Container elements always follow a strict order. All the elements are inserted in this order a given position.
Map Each element will be a key to associate a mapping value: indicates the key identification element to the main content mapped values.
Key The container of any two elements can have the same key.
Allocator-aware Using a dispenser to a container object is handled dynamically its storage requirements.

 

Template parameters:

Key

The type of bond. Map of each element by its unique identification key.

Alias ​​is a member of type mapping :: key_type.

T

Type values ​​map. Mapping each element will store some data for mapping values.

Alias ​​is a member of type mapping :: mapped_type.

Compare

In two key elements as arguments and returns a bool value. Expression compare (a, b) in, compare this type of object, a and b are the key, if a weak ordering function strictly defined, a is b is considered before, expression comp (a, b ) should return true.

map object using this expression and to determine the order of the elements in the two key elements of the container are equal (compare them by reflex:!! If compare (a, b) && compare (b, a), they are equal ). map container of any two elements can have the same key.

It can be a function pointer, the function may be an object (see constructor). This defaults to less <T>, it returns the results of the application is smaller than the operator (a <b) the same.

Alias ​​is a member of type mapping :: key_compare.

Alloc

The dispenser of the type defined in the object storage allocation model. By default, using the dispenser class template, which defines the most simple memory allocation model, and the value is irrelevant.

Alias ​​is a member of type mapping :: allocator_type.

 

Member Type:

C++11:

member type definition notes
key_type The first template parameter (Key)  
mapped_type The second template parameter (T)  
value_type pair<const key_type,mapped_type>  
key_compare The third template parameter (Compare) defaults to: less<key_type>
value_compare Nested function class to compare elements see value_comp
allocator_type The fourth template parameter (Alloc) defaults to: allocator<value_type>
reference value_type&  
const_reference const value_type&  
pointer allocator_traits<allocator_type>::pointer for the default allocator: value_type*
const_pointer allocator_traits<allocator_type>::const_pointer for the default allocator: const value_type*
iterator bidirectional iterator to value_type convertible to const_iterator
const_iterator bidirectional iterator to const value_type  
reverse_iterator reverse_iterator<iterator>  
const_reverse_iterator reverse_iterator<const_iterator>  
difference_type a signed integral type, identical to:
iterator_traits<iterator>::difference_type
usually the same as ptrdiff_t
size_type an unsigned integral type that can represent any non-negative value of difference_type usually the same as size_t

 

成员函数:

(constructor) Construct map (public member function )
(destructor) Construct map (public member function )
operator= Copy container content (public member function )

迭代器:

begin Return iterator to beginning (public member function )
end Return iterator to end (public member function )
rbegin Return reverse iterator to reverse beginning (public member function )
rend Return reverse iterator to reverse end (public member function )
cbegin  Return const_iterator to beginning (public member function )
cend  Return const_iterator to end (public member function )
crbegin  Return const_reverse_iterator to reverse beginning (public member function )
crend  Return const_reverse_iterator to reverse end (public member function )

容量:

empty Test whether container is empty (public member function )
size Return container size (public member function )
max_size Return maximum size (public member function )

获取元素:

operator[] Access element (public member function )
at  Access element (public member function )

修改:

insert Insert elements (public member function )
erase Erase elements (public member function )
swap Swap content (public member function )
clear Clear content (public member function )
emplace  Construct and insert element (public member function )
emplace_hint  Construct and insert element with hint (public member function )

Observers:

key_comp Return key comparison object (public member function )
value_comp Return value comparison object (public member function )

操作:

find Get iterator to element (public member function )
count Count elements with a specific key (public member function )
lower_bound Return iterator to lower bound (public member function )
upper_bound Return iterator to upper bound (public member function )
equal_range Get range of equal elements (public member function )

Allocator:

get_allocator Get allocator (public member function )

 

本文内容来源:http://www.cplusplus.com/reference/map/map/

发布了16 篇原创文章 · 获赞 9 · 访问量 8537

Guess you like

Origin blog.csdn.net/qq_39025293/article/details/104486021