标签:
参见http://www.cplusplus.com/reference/map/multimap/
多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multimap在某些情况下会更有用,比如说在电话簿中同一个人可以有多个电话号码
multimap中并没有像map那样提供重载的operator[],因此不能通过operator[]给元素赋值
template < class Key, // multimap::key_type
class T, // multimap::mapped_type
class Compare = less<Key>, // multimap::key_compare
class Alloc = allocator<pair<const Key,T> > // multimap::allocator_type
> class multimap;Multiple-key map
Multimaps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order, and where multiple elements can have equivalent keys.
[multimap是关联容器,其中的元素也是通过key value和mapped value根据指定的排序组织起来的,multimap允许重复元素]
In a multimap, the key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key. The types of key and mapped value may differ, and are grouped together in member type value_type, which is a pair type combining both:
[在map中,key value一般被用来对元素进行排序以及唯一确定元素,而mapped value则用来存储对应key value所关联的内容。key value和mapped value的数据类型可能不一样,但它们都是通过成员类型value_type组织起来的,value_type是一个pair类型,其定义如下:]
typedef pair<const Key, T> value_type;
Internally, the elements in a multimap are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
[multimap的内部实现中,元素总是根据key value来进行排序的,排序是通过内部的比较对象(internal comparison object)进行严格弱化排序]
Multimaps are typically implemented as binary search trees.
[multimap的典型实现是通过二进制搜索树]
/* //constructing multimaps multimap (const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); multimap (InputIterator first, InputIterator last, const key_compare& comp = key_compare(), const allocator_type& alloc = allocator_type()); multimap (const multimap& x); iterator begin(); iterator end(); reverse_iterator rbegin(); reverse_iterator rend(); void clear(); bool empty() const; size_type size() const; void swap(multimap& x);
multimap& operator=(const multimap& x);
//multimap::count size_type count(const key_type& k) const; Count elements with a specific key Searches the container for elements with a key equivalent to k and returns the number of matches. [查找容器中与key value与k相等的元素个数] */ #include <iostream> #include <map> #include <string> typedef std::pair<std::string, std::string> PAIR; int main() { std::multimap<std::string, std::string> phone; phone.insert(PAIR("John", "0106228542")); phone.insert(PAIR("Mary", "0108225634")); phone.insert(PAIR("Mary", "8708532")); phone.insert(PAIR("Mary", "13583852314")); int count = phone.count("Mary"); std::cout<<"There stores "<<count<<" Mary‘s phone number"<<std::endl; system("pause"); return 0; }
/* iterator insert(const value_type& val); iterator insert(iterator position, const value_type& val); void insert(InputIterator first, InputIterator last); Insert element Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted. [通过插入的元素来有效地增加容器大小] Internally, multimap containers keep all their elements sorted by key following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering. [在内部实现中,multimap容器会用比较对象(comparison object)根据key value将元素进行排序,因此插入操作也是根据这一点插入到相对位置] Return value In the versions returning a value, this is an iterator pointing to the newly inserted element in the multiset. [该函数返回一个指向新插入元素的迭代器(如果有返回值的话)] Member type iterator is a bidirectional iterator type that points to elements. [成员类型iterator是一个指向元素的双向迭代器类型] 注: position Hint for the position where the element can be inserted. [position的作用是建议元素被插入的位置] The function optimizes its insertion time if position points to the element that will precede the inserted element. [如果position对应的元素在插入元素之前的话,该函数会优化插入时间] Notice that this is just a hint and does not force the new element to be inserted at that position within the multimap container (the elements in a multimap always follow a specific order depending on their key). [要注意的是,position只是建议元素的插入位置,而不是强制,另外,multimap中的元素会根据前面所述方法进行排序] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymultimap; std::multimap<char,int>::iterator it; // first insert function version (single parameter): mymultimap.insert ( std::pair<char,int>(‘a‘,100) ); mymultimap.insert ( std::pair<char,int>(‘z‘,150) ); it=mymultimap.insert ( std::pair<char,int>(‘b‘,75) ); // second insert function version (with hint position): mymultimap.insert (it, std::pair<char,int>(‘c‘,300)); // max efficiency inserting mymultimap.insert (it, std::pair<char,int>(‘z‘,400)); // no max efficiency inserting // third insert function version (range insertion): std::multimap<char,int> anothermultimap; anothermultimap.insert(mymultimap.begin(),mymultimap.find(‘c‘)); // showing contents: std::cout << "mymultimap contains:\n"; for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << (*it).first << " => " << (*it).second << ‘\n‘; std::cout << "anothermultimap contains:\n"; for (it=anothermultimap.begin(); it!=anothermultimap.end(); ++it) std::cout << (*it).first << " => " << (*it).second << ‘\n‘; system("pause"); return 0; }
/* void erase (iterator position); size_type erase (const key_type& k); void erase (iterator first, iterator last); Return value For the key-based version (2), the function returns the number of elements erased. [第二种方式会返回被删除元素的个数] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymultimap; // insert some values: mymultimap.insert(std::pair<char,int>(‘a‘,10)); mymultimap.insert(std::pair<char,int>(‘b‘,20)); mymultimap.insert(std::pair<char,int>(‘b‘,30)); mymultimap.insert(std::pair<char,int>(‘c‘,40)); mymultimap.insert(std::pair<char,int>(‘d‘,50)); mymultimap.insert(std::pair<char,int>(‘d‘,60)); mymultimap.insert(std::pair<char,int>(‘e‘,70)); mymultimap.insert(std::pair<char,int>(‘f‘,80)); std::multimap<char,int>::iterator it = mymultimap.find(‘b‘); mymultimap.erase (it); // erasing by iterator (1 element) mymultimap.erase (‘d‘); // erasing by key (2 elements) it=mymultimap.find (‘e‘); mymultimap.erase ( it, mymultimap.end() ); // erasing by range // show content: for (it=mymultimap.begin(); it!=mymultimap.end(); ++it) std::cout << (*it).first << " => " << (*it).second << ‘\n‘; system("pause"); return 0; }
/* iterator find(const key_type& k); Get iterator to element Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to multimap::end. [查找第一个key value为k的元素并返回指向该元素的迭代器,如果没有找到则返回指向multimap::end的迭代器] Notice that this function returns an iterator to a single element (of the possibly multiple elements with equivalent keys). To obtain the entire range of equivalent elements, see multimap::equal_range. [需要注意的是,该函数返回的是指向单个元素的迭代器。如果要获取与指定元素相等的区间的迭代器,请参看multimap::equal_range] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymm; mymm.insert (std::make_pair(‘x‘,10)); mymm.insert (std::make_pair(‘y‘,20)); mymm.insert (std::make_pair(‘z‘,30)); mymm.insert (std::make_pair(‘z‘,40)); std::multimap<char,int>::iterator it = mymm.find(‘x‘); mymm.erase (it); mymm.erase (mymm.find(‘z‘)); // print content: std::cout << "elements in mymm:" << ‘\n‘; std::cout << "y => " << mymm.find(‘y‘)->second << ‘\n‘; std::cout << "z => " << mymm.find(‘z‘)->second << ‘\n‘; system("pause"); return 0; }
/* key_compare key_comp() const; Return key comparison object Returns a copy of the comparison object used by the container to compare keys. [返回容器中被用来比较key的比较对象的拷贝] By default, this is a less object, which returns the same as operator<. [默认情况下是一个less对象,该对象返回的结果如同operator<] This object determines the order of the elements in the container: it is a function pointer or a function object that takes two arguments of the same type as the element keys, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise. [比较对象决定了容器中元素的排序,比较对象是一个函数指针或者一个函数对象,该函数指针或者函数对象有两个相同类型的参数,如果按照严格弱排序,第一个参数排在第二个参数之前则返回true,否则返回false] Two keys are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the keys are passed as arguments). [如果key_comp()返回false则认为这两个key是相等的] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymultimap; std::multimap<char,int>::key_compare mycomp = mymultimap.key_comp(); mymultimap.insert (std::make_pair(‘a‘,100)); mymultimap.insert (std::make_pair(‘b‘,200)); mymultimap.insert (std::make_pair(‘b‘,211)); mymultimap.insert(std::make_pair(‘b‘, 190)); mymultimap.insert(std::make_pair(‘b‘, 180)); mymultimap.insert (std::make_pair(‘c‘,300)); std::cout << "mymultimap contains:\n"; char highest = mymultimap.rbegin()->first; // key value of last element std::multimap<char,int>::iterator it = mymultimap.begin(); do { std::cout << (*it).first << " => " << (*it).second << ‘\n‘; } while ( mycomp((*it++).first, highest) ); std::cout << ‘\n‘; system("pause"); return 0; }
/* value_compare value_comp() const; Return value comparison object Returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second. 返回一个用来排序的比较对象] The arguments taken by this function object are of member type value_type (defined in multimap as an alias of pair<const key_type,mapped_type>), but the mapped_type part of the value is not taken into consideration in this comparison. [该函数的参数类型是value_type(其被定义为pair<const key_type, mapped_type>的别名),但mapped_type部分的值不会参与比较(也就是说参与比较的只有key value)] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymultimap; mymultimap.insert(std::make_pair(‘x‘,101)); mymultimap.insert(std::make_pair(‘y‘,202)); mymultimap.insert(std::make_pair(‘y‘,252)); mymultimap.insert(std::make_pair(‘z‘,303)); std::cout << "mymultimap contains:\n"; std::pair<char,int> highest = *mymultimap.rbegin(); // last element std::multimap<char,int>::iterator it = mymultimap.begin(); do { std::cout << (*it).first << " => " << (*it).second << ‘\n‘; } while ( mymultimap.value_comp()(*it++, highest) ); system("pause"); return 0; }
/* iterator lower_bound (const key_type& k); //返回键值>=k的第一个元素的位置 iterator upper_bound (const key_type& k); //返回键值>k的第一个元素的位置 Return iterator to lower bound Returns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after). [返回一个指向容器中第一个key value不在k之前的元素的迭代器(即指向元素的key value等于或大于k)] The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element_key,k) would return false. [该函数利用内部的比较对象来比较,返回的迭代器指向的是第一个使得key_comp(element_key, k)返回false的元素] If the multimap class is instantiated with the default comparison type (less), the function returns an iterator to the first element whose key is not less than k. [如果multimap类的实例化使用的是默认的比较类型(less), 则该函数返回的迭代器指向的是第一个key value不小于(即大于等于)k的元素] A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the multimap contains elements with keys equivalent to k: In this case, lower_bound returns an iterator pointing to the first of such elements, whereas upper_bound returns an iterator pointing to the element following the last. [upper_bound函数的行为与lower_bound相同,只不过upper_bound返回的迭代指向的是第一个key value大于k的元素] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymultimap; std::multimap<char,int>::iterator it,itlow,itup; mymultimap.insert(std::make_pair(‘a‘,10)); mymultimap.insert(std::make_pair(‘b‘,121)); mymultimap.insert(std::make_pair(‘c‘,1001)); mymultimap.insert(std::make_pair(‘c‘,2002)); mymultimap.insert(std::make_pair(‘d‘,11011)); mymultimap.insert(std::make_pair(‘e‘,44)); itlow = mymultimap.lower_bound (‘b‘); // itlow points to b itup = mymultimap.upper_bound (‘d‘); // itup points to e (not d) // print range [itlow,itup): for (it=itlow; it!=itup; ++it) std::cout << (*it).first << " => " << (*it).second << ‘\n‘; system("pause"); return 0; }
/* pair<iterator,iterator> equal_range (const key_type& k); Get range of equal elements Returns the bounds of a range that includes all the elements in the container which have a key equivalent to k. [返回包含所有与k相等的元素的区间] If no matches are found, the range returned has a length of zero, with both iterators pointing to the first element that has a key considered to go after k according to the container‘s internal comparison object (key_comp). [如果没有匹配元素,该区间长度为0,且两个迭代器都会指向容器中第一个大于k的元素] Return value The function returns a pair, whose member pair::first is the lower bound of the range (the same as lower_bound), and pair::second is the upper bound (the same as upper_bound). [该函数返回一个pair,其中first指的是区间的lower bound,second指的是区间的upper bound] */ #include <iostream> #include <map> int main () { std::multimap<char,int> mymm; mymm.insert(std::pair<char,int>(‘a‘,10)); mymm.insert(std::pair<char,int>(‘b‘,20)); mymm.insert(std::pair<char,int>(‘b‘,30)); mymm.insert(std::pair<char,int>(‘b‘,40)); mymm.insert(std::pair<char,int>(‘c‘,50)); mymm.insert(std::pair<char,int>(‘c‘,60)); mymm.insert(std::pair<char,int>(‘d‘,60)); std::cout << "mymm contains:\n"; for (char ch=‘a‘; ch<=‘d‘; ch++) { std::pair <std::multimap<char,int>::iterator, std::multimap<char,int>::iterator> ret; ret = mymm.equal_range(ch); std::cout << ch << " =>"; for (std::multimap<char,int>::iterator it=ret.first; it!=ret.second; ++it) std::cout << ‘ ‘ << it->second; std::cout << ‘\n‘; } system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/kevinq/p/4544783.html