In their implementation in the C++ Standard Template Library, map containers take four template parameters:

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 the key type and returns a bool. The expression comp(a,b), where comp is an object of this comparison class and a and b are key values, shall return true 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 a function call operator or a pointer to a function (see constructor for an example). This defaults to less<Key>, which returns the same as applying the less-than operator (a<b).
    The map object uses this expression to determine the position of the elements in the container. All elements in a map 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.

 

// constructing maps 
#include <iostream> 
#include <map> using namespace std; 
bool fncomp (char lhs, char rhs) {return lhs<rhs;} 
struct classcomp 
bool operator() (const char& lhs, const char& rhs) const 
{
return lhs<rhs;
}; 
int main () 
{
map<char,int> first; 
first['a']=10; first['b']=30; 
first['c']=50; first['d']=70; 
map<char,int> second (first.begin(),first.end()); 
map<char,int> third (second); 
map<char,int,classcomp> fourth; // class as Compare 
bool(*fn_pt)(char,char) = fncomp; 
map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare 
return 0; 
}

 

ref : http://www.cplusplus.com/reference/stl/map/

ref : http://www.cplusplus.com/reference/stl/map/map/

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()