map
来源:互联网 发布:python将变量写入txt 编辑:程序博客网 时间:2024/05/29 12:32
http://www.cplusplus.com/reference/stl/map/
<map>
Map
Maps are a kind of associative container that stores elements formed by the combination of akey value and a mapped value.In a map, the key value is generally used to uniquely identify the element, while themapped value is some sort of value associated to this key. Types ofkey and mapped value may differ. For example, a typical example of a map is a telephone guide where the name is thekey and the telephone number is the mapped value.
Internally, the elements in the map are sorted from lower to higher key value following a specific strict weak ordering criterion set on construction.
As associative containers, they are especially designed to be efficient accessing its elements by theirkey (unlike sequence containers, which are more efficient accessing elements by their relative or absolute position).
Therefore, the main characteristics of a map as an associative container are:
- Unique key values: no two elements in the map have keys that compare equal to each other. For a similar associative container allowing for multiple elements with equivalent keys, seemultimap.
- Each element is composed of a key and a mapped value. For a simpler associative container where the element value itself is its key, seeset.
- Elements follow a strict weak ordering at all times. Unordered associative arrays, likeunordered_map, are available in implementations following TR1.
Maps are also unique among associative containers in that they implement the direct access operator (operator[]) which allows for direct access of the mapped value.
In their implementation in the C++ Standard Template Library, map containers take four template parameters:
12
template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map;
Where the template parameters have the following meanings:
- Key: Type of the key values. Each element in a map is uniquely identified by its key value.
- T: Type of the mapped value. Each element in a map is used to store some data as its mapped value.
- Compare: Comparison class: A class that takes two arguments of thekey type and returns a bool. The expression comp(a,b), wherecomp is an object of this comparison class and a and b are key values, shall returntrue if a is to be placed at an earlier position than b in a strict weak ordering operation. This can either be a class implementing afunction call operator or a pointer to a function (see constructor for an example). This defaults to less<Key>, which returns the same as applying theless-than operator (a<b).
The map object uses this expression to determine the position of the elements in the container. All elements in amap container are ordered following this rule at all times. - Allocator: Type of the allocator object used to define the storage allocation model. By default, theallocator class template is used, which defines the simplest memory allocation model and is value-independent.
This container class supports bidirectional iterators.
Iterators to elements of map containers access to both the key and themapped value. For this, the class defines what is called its value_type, which is apair class with its first value corresponding to theconst version of the key type (template parameter Key) and itssecond value corresponding to the mapped value (template parameterT):
typedef pair<const Key, T> value_type;
Iterators of a map container point to elements of this value_type. Thus, for an iterator calledit that points to an element of a map, its key and mapped value can be accessed respectively with:
1234
map<Key,T>::iterator it;(*it).first; // the key value (of type Key)(*it).second; // the mapped value (of type T)(*it); // the "element value" (of type pair<const Key,T>)
Naturally, any other direct access operator, such as -> or [] can be used, for example:
12
it->first; // same as (*it).first (the key value)it->second; // same as (*it).second (the mapped value)
Member functions
- (constructor)
- Construct map (public member function)
- (destructor)
- Map destructor (public member function)
- operator=
- Copy container content (public member function)
Iterators:
- 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)
Capacity:
- empty
- Test whether container is empty (public member function)
- size
- Return container size (public member function)
- max_size
- Return maximum size (public member function)
Element access:
- operator[]
- Access element (public member function )
Modifiers:
- insert
- Insert elements (public member function )
- erase
- Erase elements (public member function)
- swap
- Swap content (public member function)
- clear
- Clear content (public member function)
Observers:
- key_comp
- Return key comparison object (public member function)
- value_comp
- Return value comparison object (public member function)
Operations:
- 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 )
Member types
of template <class Key, class T, class Compare=less<Key>, class Allocator=allocator<pair <const Key, T> > > class map;- map
- MAP
- map
- MAP
- map
- map
- map
- Map
- Map
- Map
- map
- map
- Map
- .map
- map
- MAP
- map
- Map
- aspnetpager 分页存储过程的实现
- Excel中快速分列
- java与c/c++
- map
- java内存处理问题
- map
- vim打开文件后乱码的问题
- C++中引用传递与指针传递区别(进一步整理)
- Hulu宣讲会小记(9月5日计算所)
- Spring MVC 3 深入总结
- More Effective C++:指针与引用的区别
- 在使用delphi中对DOUBLE类型比较问题
- C++ STL map的使用
- Windows Store apps开发[20]添加应用设置(SettingsPane)