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

cplusplus


C++ std::multimap的更多相关文章

  1. std::multimap

    标准库还定义了一个 multimap 容器,它与 map 类似,所不同的是它允许重复键. 成员函数 insert() make_pair() 辅助函数来完成此任务. find(k) 返回指向第一个与键 ...

  2. std::multimap 按照key遍历---

    #include <iostream> #include <unordered_map> using namespace std; int main() { unordered ...

  3. STL之multimap

    参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multi ...

  4. [代码]multimap员工分组案例

    案例要求: //multimap 案例//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作//人员信息有: 姓名 年龄 电话 工资等组成//通过 Multimap 进行信 ...

  5. multimap详讲

    multimap和map的区别: 首先认识一下multimap和map的区别: 1>        multimap不提供operator[ ]运算符.因为这个运算符的语义在同一个键可以保存多个 ...

  6. multimap的使用 in C++,同一个关键码存在多个值

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  7. 编程杂谈——std::vector与List<T>的性能比较

    昨天在比较完C++中std::vector的两个方法的性能差异并留下记录后--编程杂谈--使用emplace_back取代push_back,今日尝试在C#中测试对应功能的性能. C#中对应std:: ...

  8. std::map使用结构体自定义键值

    使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...

  9. multimap遍历与查找

    std::multimap<int, std::string> m; m.insert(std::make_pair(0, "w0")); m.insert(std:: ...

随机推荐

  1. 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念

    一.前言     DDD(领域驱动设计)的一些介绍网上资料很多,这里就不继续描述了.自己使用领域驱动设计摸滚打爬也有2年多的时间,出于对知识的总结和分享,也是对自我理解的一个公开检验,介于博客园这个平 ...

  2. .NET Core中间件的注册和管道的构建(1)---- 注册和构建原理

    .NET Core中间件的注册和管道的构建(1)---- 注册和构建原理 0x00 问题的产生 管道是.NET Core中非常关键的一个概念,很多重要的组件都以中间件的形式存在,包括权限管理.会话管理 ...

  3. 说说Golang的使用心得

    13年上半年接触了Golang,对Golang十分喜爱.现在是2015年,离春节还有几天,从开始学习到现在的一年半时间里,前前后后也用Golang写了些代码,其中包括业余时间的,也有产品项目中的.一直 ...

  4. 【java】Naming.bind和Registry.bind区别

    Naming类和Registry类均在java.rmi包 Naming类通过解析URI绑定远程对象,将URI拆分成主机.端口和远程对象名称,使用的仍是Registry类. public static ...

  5. [Java 缓存] Java Cache之 DCache的简单应用.

    前言 上次总结了下本地缓存Guava Cache的简单应用, 这次来继续说下项目中使用的DCache的简单使用. 这里分为几部分进行总结, 1)DCache介绍; 2)DCache配置及使用; 3)使 ...

  6. HTTP常用状态码分析

    不管是面试还是工作中,经常会碰到需要通过HTTP状态码去判断问题的情况,比如对于后台RD,给到前端FE的一个接口,出现502或者504 error错误,FE就会说接口存在问题,如果没有知识储备,那就只 ...

  7. 讓TQ2440也用上設備樹(1)

    作者:彭東林 郵箱:pengdonglin137@163.com QQ:405728433 開發板 TQ2440 + 64MB 內存 + 256MB Nand 軟件 Linux: Linux-4.9 ...

  8. Oracle手边常用70则脚本知识汇总

    Oracle手边常用70则脚本知识汇总 作者:白宁超 时间:2016年3月4日13:58:36 摘要: 日常使用oracle数据库过程中,常用脚本命令莫不是用户和密码.表空间.多表联合.执行语句等常规 ...

  9. iOS开发--ChildViewController实现订单页的切换

    先不说废话, 上效果图, 代码量也不大, 也不上传github骗星星了, 你们复制粘贴下代码, 就可以轻而易举的弄出一个小demo. 这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, ...

  10. jQuery 的选择器常用的元素查找方法

    jQuery 的选择器常用的元素查找方法 基本选择器: $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myE ...