C++ std::multimap
std::multimap
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.
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:
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 containers are generally slower than unordered_multimap containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
Multimaps are typically implemented as binary search trees.
Container properties
- Associative: Elements in associative containers are referenced by their key and not by their absolute position in the container.
- Ordered: The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.
- Map: Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.
- Multiple equivalent keys: Multiple elements in the container can have equivalent keys.
- Allocator-aware: The container uses an allocator object to dynamically handle its storage needs.
Template parameters
- Key: Type of the keys. Each element in a map is identified by its key value. Aliased as member type multimap::key_type.
- T: Type of the mapped value. Each element in a multimap stores some data as its mapped value. Aliased as member type multimap::mapped_type.
- Compare: A binary predicate that takes two element keys as arguments and returns a bool. The expression comp(a,b), where comp is an object of this type and a and b are element keys, shall return true if a is considered to go before b in the strict weak ordering the function defines. The multimap object uses this expression to determine both the order the elements follow in the container and whether two element keys are equivalent (by comparing them reflexively: they are equivalent if !comp(a,b) && !comp(b,a)). This can be a function pointer or a function object (see constructor for an example). This defaults to less, which returns the same as applying the less-than operator (a<b). Aliased as member type multimap::key_compare.
- Alloc: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Aliased as member type multimap::allocator_type.
Multimap 的函数和map的相同, 所以就不记录了. 直接写一些代码例子, Map的详细说明, 参见这里;
Code
#include <iostream>
#include <map>
bool fncomp(char lhs,char rhs)
{ return lhs < rhs; }
struct classcomp
{
bool operator() (const char &lhs, const char &rhs)
{ return lhs < rhs; }
};
int main(int argc, char **argv)
{
multimap<char,int> first;
first.insert( pair<char,int>('a',10) );
first.insert( pair<char,int>('b',20) );
first.insert( pair<char,int>('b',20) );
first.insert( pair<char,int>('c',30) );
multimap<char,int> first1( first.begin(), begin.end());
multimap<char,int> first2(first1);
multimap<char,int, classcomp> first3;
bool (* fn_pt)(char ,char) =fncomp;
multimap<char,int, bool(*)(char,char)> first4(fn_pt);
return 0;
}
Reference
C++ std::multimap的更多相关文章
- std::multimap
标准库还定义了一个 multimap 容器,它与 map 类似,所不同的是它允许重复键. 成员函数 insert() make_pair() 辅助函数来完成此任务. find(k) 返回指向第一个与键 ...
- std::multimap 按照key遍历---
#include <iostream> #include <unordered_map> using namespace std; int main() { unordered ...
- STL之multimap
参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multi ...
- [代码]multimap员工分组案例
案例要求: //multimap 案例//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作//人员信息有: 姓名 年龄 电话 工资等组成//通过 Multimap 进行信 ...
- multimap详讲
multimap和map的区别: 首先认识一下multimap和map的区别: 1> multimap不提供operator[ ]运算符.因为这个运算符的语义在同一个键可以保存多个 ...
- multimap的使用 in C++,同一个关键码存在多个值
#include <iostream> #include <string> #include <vector> #include <algorithm> ...
- 编程杂谈——std::vector与List<T>的性能比较
昨天在比较完C++中std::vector的两个方法的性能差异并留下记录后--编程杂谈--使用emplace_back取代push_back,今日尝试在C#中测试对应功能的性能. C#中对应std:: ...
- std::map使用结构体自定义键值
使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...
- multimap遍历与查找
std::multimap<int, std::string> m; m.insert(std::make_pair(0, "w0")); m.insert(std:: ...
随机推荐
- Jexus 5.8.2 正式发布为Asp.Net Core进入生产环境提供平台支持
Jexus 是一款运行于 Linux 平台,以支持 ASP.NET.PHP 为特色的集高安全性和高性能为一体的 WEB 服务器和反向代理服务器.最新版 5.8.2 已经发布,有如下更新: 1,现在大 ...
- C语言 · 4_2找公倍数
问题描述 这里写问题描述. 打印出1-1000所有11和17的公倍数. 样例输入 一个满足题目要求的输入范例.例:无 样例输出 与上面的样例输入对应的输出.例: 代码如下: #include< ...
- React的使用与JSX的转换
前置技能:Chrome浏览器 一.拿糖:React的使用 React v0.14 RC 发布,主要更新项目: 两个包: React 和 React DOM DOM node refs 无状态的功能 ...
- Objective-C中block的底层原理
先出2个考题: 1. 上面打印的是几,captureNum2 出去作用域后是否被销毁?为什么? 同样类型的题目: 问:打印的数字为多少? 有人会回答:mutArray是captureObject方法的 ...
- 9、 Struts2验证(声明式验证、自定义验证器)
1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证: ...
- 玩转spring boot——结合JPA入门
参考官方例子:https://spring.io/guides/gs/accessing-data-jpa/ 接着上篇内容 一.小试牛刀 创建maven项目后,修改pom.xml文件 <proj ...
- c#语言规范
0x00 分类 C#语言规范主要有两个来源,即我们熟知的ECMA规范和微软的规范.尽管C#的ECMA规范已经前后修订4次,但其内容仅仅到C# 2.0为止.所以慕容为了方便自己和各位方便查询,在此将常见 ...
- 浅谈java异常[Exception]
学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...
- 《如何使用Javascript判断浏览器终端设备》
WEB开发中如何通过Javascript来判断终端为PC.IOS(iphone).Android呢? 可以通过判断浏览器的userAgent,用正则来判断手机是否是ios和Android客户端. va ...
- Linux学习
Linux 命令英文全称su:Swith user 切换用户,切换到root用户cat: Concatenate 串联uname: Unix name 系统名称df: Disk free 空余硬盘du ...