参见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", ""));
phone.insert(PAIR("Mary", ""));
phone.insert(PAIR("Mary", ""));
phone.insert(PAIR("Mary", "")); int count = phone.count("Mary"); std::cout<<"There stores "<<count<<" Mary's phone number"<<std::endl; system("pause");
return ;
}
/*
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',) );
mymultimap.insert ( std::pair<char,int>('z',) );
it=mymultimap.insert ( std::pair<char,int>('b',) ); // second insert function version (with hint position):
mymultimap.insert (it, std::pair<char,int>('c',)); // max efficiency inserting
mymultimap.insert (it, std::pair<char,int>('z',)); // 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 ;
}
/*
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',));
mymultimap.insert(std::pair<char,int>('b',));
mymultimap.insert(std::pair<char,int>('b',));
mymultimap.insert(std::pair<char,int>('c',));
mymultimap.insert(std::pair<char,int>('d',));
mymultimap.insert(std::pair<char,int>('d',));
mymultimap.insert(std::pair<char,int>('e',));
mymultimap.insert(std::pair<char,int>('f',)); 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 ;
}
/*
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',));
mymm.insert (std::make_pair('y',));
mymm.insert (std::make_pair('z',));
mymm.insert (std::make_pair('z',)); 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 ;
}
/*
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',));
mymultimap.insert (std::make_pair('b',));
mymultimap.insert (std::make_pair('b',));
mymultimap.insert(std::make_pair('b', ));
mymultimap.insert(std::make_pair('b', ));
mymultimap.insert (std::make_pair('c',)); 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 ;
}
/*
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',));
mymultimap.insert(std::make_pair('y',));
mymultimap.insert(std::make_pair('y',));
mymultimap.insert(std::make_pair('z',)); 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 ;
}
/*
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',));
mymultimap.insert(std::make_pair('b',));
mymultimap.insert(std::make_pair('c',));
mymultimap.insert(std::make_pair('c',));
mymultimap.insert(std::make_pair('d',));
mymultimap.insert(std::make_pair('e',)); 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 ;
}
/*
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',));
mymm.insert(std::pair<char,int>('b',));
mymm.insert(std::pair<char,int>('b',));
mymm.insert(std::pair<char,int>('b',));
mymm.insert(std::pair<char,int>('c',));
mymm.insert(std::pair<char,int>('c',));
mymm.insert(std::pair<char,int>('d',)); 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 ;
}

STL之multimap的更多相关文章

  1. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  2. C++ STL 之 multimap案例之员工分组

    #include <iostream> #include <vector> #include <map> #include <string> #incl ...

  3. 2.9 C++STL map/multimap容器详解

    文章目录 2.9.1 引入 2.9.2 代码示例 map案列 multimap案列 2.9.3 代码运行结果 总结 2.9.1 引入 map相对于set区别,map具有键值和实值,所有元素根据键值自动 ...

  4. 【黑马程序员C++ STL】学习记录

    黑马程序员 2017 C++ STL 教程(STL 部分已完结) 基于黑马STL课程整理:黑马程序员2017C++STL教程 视频链接 专栏:本STL专栏目录 文章目录 黑马程序员 2017 C++ ...

  5. json-c与树

    json是一种轻量级的数据交换格式,因为其灵巧使得其在应用层开发以及接口层开发使用的十分广泛,最常用的莫过于协议交流使用json数据,比xml轻巧,又比二进制数据有规则.无论是各大公司的开放平台或者是 ...

  6. C++使用: C++中map的基本操作和用法

    在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...

  7. STL vector+sort排序和multiset/multimap排序比较

    由 www.169it.com 搜集整理 在C++的STL库中,要实现排序可以通过将所有元素保存到vector中,然后通过sort算法来排序,也可以通过multimap实现在插入元素的时候进行排序.在 ...

  8. STL的基本使用之关联容器:map和multiMap的基本使用

    STL的基本使用之关联容器:map和multiMap的基本使用 简介 map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序.两者不同在于map ...

  9. stl vector、红黑树、set、multiset、map、multimap、迭代器失效、哈希表(hash_table)、hashset、hashmap、unordered_map、list

    stl:即标准模板库,该库包含了诸多在计算机科学领域里所常用的基本数据结构和基本算法 六大组件: 容器.迭代器.算法.仿函数.空间配置器.迭代适配器 迭代器:迭代器(iterator)是一种抽象的设计 ...

随机推荐

  1. (笔记)angular 的hover事件

  2. JavaScript for...in 语句

    JavaScript for...in 语句 for...in 语句用于遍历数组或者对象的属性(对数组或者对象的属性进行循环操作). for ... in 循环中的代码每执行一次,就会对数组的元素或者 ...

  3. dig out deledted chat messages

    One of my friends asked me to do a favor for her. She said her friend deleted some important chat me ...

  4. TP-Link访问策略

    参考资料: http://service.tp-link.com.cn/detail_article_111.html

  5. PowerDesigner英文字段转换中文字段显示

    get_comments.vbs代码如下点击Run Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' ...

  6. Nginx 日志按天分割

    #nginx日志切割脚本 #!/bin/bash #设置日志文件存放目录 logs_path="/home/www.xxx.com/wwwlogs/" #设置pid文件 pid_p ...

  7. s3c6410_u-boot-2010.03移植

    开发环境: 开发板 FriendlyARM Tiny6410 主机 CentOS release 6.4 (Final) 参考: http://www.cnblogs.com/plinx/archiv ...

  8. 搭建高性能计算环境(八)、应用软件的安装之gromacs

    1,下载安装新版本的gcc(高版本的gcc只是推荐使用的,系统自带的gcc4.4照样能正常编译.运行) wget http://ftp.tsukuba.wide.ad.jp/software/gcc/ ...

  9. oracle中=>是什么意思

    => 是 Oracle 中调用 存储过程的时候, 指定 参数名进行调用.一般是, 某些参数有默认值的时候,你需要跳过某些参数来进行调用.下面是具体的例子. 参数的默认值SQL> CREAT ...

  10. 百度搜索API v3版本与soap

    技术文档请参考http://dev2.baidu.com/docs.do?product=2#page=File,和http://dev2.baidu.com/docs.do?product=2#pa ...