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. Travis CI用来持续集成你的项目

    这里持续集成基于GitHub搭建的博客为项目 工具: zqz@ubuntu:~$ node --version v4.2.6 zqz@ubuntu:~$ git --version git versi ...

  2. 重撸JS_1

    1.声明 用 var 或 let 声明的未赋初值的变量,值会被设定为undefined(译注:即未定义值,本身也是一个值) 试图访问一个未初始化的变量会导致一个 ReferenceError 异常被抛 ...

  3. InnoDB体系结构学习笔记

    后台线程 Master Thread 核心的后台线程,主要负责将缓冲池的数据异步刷新到磁盘,保证数据的一致性,包括(脏页的刷新).合并插入缓冲.(UNDO页的回收)等 IO Thread 4个writ ...

  4. [C#] 简单的 Helper 封装 -- SQLiteHelper

    using System; using System.Data; using System.Data.SQLite; namespace SqliteConsoleApp { /// <summ ...

  5. mongodb

    修改所有的记录: > db.t_express_apply.update({},{$set:{"isStatus" : 0}},{multi:true})WriteResul ...

  6. Lind.DDD.LindAspects方法拦截的介绍

    回到目录 什么是LindAspects 之前写了关于Aspects的文章<Lind.DDD.Aspects通过Plugins实现方法的动态拦截~Lind里的AOP>,今天主要在设计思想上进 ...

  7. nginx服务器安装及配置文件详解

    nginx在工作中已经有好几个环境在使用了,每次都是重新去网上扒博客,各种编译配置,今天自己也整理一份安装文档和nginx.conf配置选项的说明,留作以后参考.像负载均衡配置(包括健康检查).缓存( ...

  8. iOS之应用版本号的设置规则

    版本号的格式:v<主版本号>.<副版本号>.<发布号>  版本号的初始值:v1.0.0 管理规则: 主版本号(Major version) 1.  产品的主体构件进 ...

  9. javascript运动学教程

    本文系笔者学习原生javascript动效的笔记.内容基于某非著名培训机构的视频教程.并重新做了归类整理.删除了一些过时的内容.并重做了GIF图,加上了自己的一些分析. 一. 运动学基础 引子:从左到 ...

  10. JS高级前端开发群加群说明及如何晋级

    JS高级前端开发群加群说明 一.文章背景: 二. 高级群: 三. 加入方式: 四. 说明:   一.文章背景: 去年年初建了几个群,在不经意间火了,一直排在“前端开发”关键字搜索结果第一名.当然取得这 ...