一、  map

头文件  #include

1)map的定义:

map m;

或者

typedef map  M;

M m;

2)元素的插入

map m;

最常用的  m[key]=value;   //m[1]=2;

m.insert(pair(1,2));

3)元素的查找

find()函数  返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map ::iterator it;

it=m.find(1);

if(it==m.end())

cout<

这里要注意一个问题

M m;

cout<

m[1];

cout<

即使我们不对m[1]赋值,这是也相当于在map容器里插入了一个数据键为1,

所以有时候判断这个元素是否存在是千万不能用m[xx]!=0因为如果不存在的话,map的大小会加1

4)元素的删除

erase

可以删除指定元素

也可以删除迭代器指定的元素

#include

#include

using namespace std;

typedef map Map;

int main()

{

Map m;

for(int i=10;i<20;i++)

m[i]=i*2;

m.erase(18);

Map::iterator it;

it=m.find(19);

if(it==m.end())

cout<

else

m.erase(it);

for(it=m.begin();it!=m.end();it++)

{

cout<first<second<

}

return 0;

}

5)swap()  完成2个map容器的交换

map中的元素按照键值的大小升序排列

6) map中可以按照键值的从大到小输出

mapm;

m[2]=1;

m[1]=2;

m[3]=7;

map::reverse_iterator it;for(it=m.rbegin();it!=m.rend();it++){

cout<first<second<

}

二 、multimap

multimap多重映照容器:容器的数据结构采用红黑树进行管理

multimap的所有元素都是pair:第一元素为键值(key),不能修改;第二元素为实值(value),可被修改

multimap特性以及用法与map完全相同,唯一的差别在于:

允许重复键值的元素插入容器(使用了RB-Tree的insert_equal函数)

因此: 键值key与元素value的映照关系是多对多的关系

没有定义[]操作运算

1)定义

multimap a;

2)插入元素

multimap a;

a.insert(pair(1,1)) ;

a.insert(pair(1,2));

a.insert(pair(1,3));

a.insert(pair(2,3));

a.insert(pair(2,4));

a.insert(pair(2,5));

a.insert(pair(3,6));

a.insert(pair(3,7));

3)删除元素

erase(key):会删除以这个key为键的所有值

4)查找元素

#include

#include

using namespace std;

typedef map Map;

int main()

{

multimap a;

a.insert(pair(1,1)) ;

a.insert(pair(1,2));

a.insert(pair(1,3));

a.insert(pair(2,3));

a.insert(pair(2,4));

a.insert(pair(2,5));

a.insert(pair(3,6));

a.insert(pair(3,7));

a.erase(1);

pair::iterator,multimap::iterator> ret;

multimap ::iterator it;

for(it=a.begin();it!=a.end();){

cout<first<

ret=a.equal_range(it->first);

for(it=ret.first;it!=ret.second;it++){

cout<

}

cout<

}

return 0;

}

如果将上面的访问容器那一部分改为:

multimap ::iterator it;

for(it=a.begin();it!=a.end();it++){

cout<first<

cout<second<

}

结果将是:

这说明我们用这种方式也可以访问。

下面是一道UVA的题目

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts)

coordinates a very successful foreign student exchange program. Over the last few years, demand

has sky-rocketed and now you need assistance with your task.

The program your organization runs works as follows: All candidates are asked for their original

location and the location they would like to go to. The program works out only if every student has

a suitable exchange partner. In other words, if a student wants to go from A to B, there must be

another student who wants to go from B to A. This was an easy task when there were only about 50

candidates, however now there are up to 500000 candidates!

Input

The input file contains multiple cases. Each test case will consist of a line containing n - the

number of candidates (1≤n≤500000), followed by n lines representing the exchange information for

each candidate. Each of these lines will contain 2 integers, separated by a single space, representing

the candidate's original location and the candidate's target location respectively. Locations will be

represented by nonnegative integer numbers. You may assume that no candidate will have his or

her original location being the same as his or her target location as this would fall into the domestic

exchange program. The input is terminated by a case where n = 0; this case should not be

processed.

Output

For each test case, print "YES" on a single line if there is a way for the exchange program to work

out, otherwise print "NO".

这是UVA 10763 题目大意是:有n个学生想要交换到其他学校,为了简单起见,规定,每个想从A到B的学生必须找一个想从B到A的搭档,学校就会同意他们的交换,给出n个学生的初始学校和他们想要换到的学校,问是否能都满足学生。

这个题目是典型的多对多的关系,所以用 multimap

#include

#include

using namespace std;

typedef multimap Map;

int main()

{

Map m;

int a,b,n;

bool flag;

while(cin>>n){

if(n==0) break;

m.clear();

for(int i=0;i

flag=true;

cin>>a>>b;

Map::iterator it;

for(it=m.find(b);it!=m.end()&&it->first==b;it++)

if(it->second==a){

flag=false;

m.erase(it);

break;

}

if(flag==true)

m.insert(pair(a,b));

}

if(m.empty())

cout<

else

cout<

}

return 0;

}

map multimapc++_C++ map 和 multimap相关推荐

  1. map multimapc++_C++的Map和Multimap

    广州C++培训的小编这一期给大家讲Map和Multimap. 6.6 Maps和Multimaps map和multimap将key/value pair当作元素进行管理.他们可根据key的排序准则自 ...

  2. map multimapc++_黑马C++视频笔记《STL之map/multimap》

    /* map/multimap容器 * map/multimap属于关联式容器,底层结构是用二叉树实现. * - map中所有元素都是pair: * - pair中第一个元素为key(键值),起到索引 ...

  3. 使用map的find头文件_C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  4. C++ 笔记(24)— STL map 类(map实例化、插入、查找、删除)

    1. STL 映射类简介 map 和 multimap 是键-值对容器,支持根据键进行查找,区别在于,后者能够存储重复的键,而前者只能存储唯一的键. 为了实现快速查找, STL map 和 multi ...

  5. java map collection_java 集合----Map、Collection

    接口:红色:实现类:黑色字体 一.Collection集合 Collection |_____Set(HashSet) |      |_____SortedSet(TreeSet) |_____Li ...

  6. java map 教程_Java Map接口

    Java Map接口 在本教程中,我们将学习Java Map接口及其方法. Java collections框架的Map接口提供了Map数据结构的功能. 它实现了Collection接口. map的工 ...

  7. java map set_java中Map、Set、List的简单使用教程(快速入门)

    Map.Set.List List的常用方法 1.创建 List list = new ArrayList<>(); List list = new LinkedList<>( ...

  8. 【Groovy】map 集合 ( 根据 Key 获取 map 集合中对应的值 | map.Key 方式 | map.‘Key’ 方式 | map[‘Key’] 方式 | 代码示例 )

    文章目录 一.根据 Key 获取 map 集合中对应的值 1.通过 map.Key 方式获取 map 集合中的值 Value 2.通过 map.'Key' 方式获取 map 集合中的值 Value 3 ...

  9. 【Flutter】Dart 数据类型 Map 类型 ( 创建 Map 集合 | 初始化 Map 集合 | 遍历 Map 集合 )

    文章目录 一. Dart 数据类型 Map 类型 二. Map 类型初始化并赋值 1. 创建 Map 对象同时进行初始化操作 2. 先创建 Map 对象再进行赋值 三. Map 集合遍历 1. 使用 ...

最新文章

  1. java查询结果自定义显示_JPA自定义对象接收查询结果集操作
  2. Python 键盘鼠标监听
  3. ii第六单元 文本处理工具
  4. 以下用于数据存储领域的python第三方库是-南开《网络爬虫与信息提取》19秋期末考核题目【标准答案】...
  5. 洛谷P3694 邦邦的大合唱
  6. 多项式全家桶学习笔记【持续更新】
  7. js slice 参数为负值
  8. Windows安装PostgreSQL11.1
  9. [BZOJ5329][Sdoi2018]战略游戏 圆方树+虚树
  10. org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
  11. Git(14)-- Git分支-- 分支管理
  12. 带你如何使用npm下载包
  13. MC下载Forge/Optifine不想有广告怎么办
  14. macos mojave_如何选择退出macOS Mojave Beta
  15. PHP 蚂蚁芝麻信用分接口
  16. i512450h和i512500h对比区别大吗
  17. JAVA通过Hutool解析CSV文件【导入即用,无需封装】
  18. Excel中如何快速地将成绩按比例来划分为等级?
  19. Shipping Grants
  20. C++的time_t 和 struct tm 类型【s

热门文章

  1. 真香警告:即使不用饿了么订餐,也请务必收藏好该库!
  2. 无线网460王者荣耀服务器,王者荣耀总是460怎么办?
  3. com组件、对象、接口
  4. 云计算机的形状,怎样用ps做任意形状的云彩
  5. Linux配置USB RNDIS
  6. excel - 遍历一块区域的方法 (TBD)
  7. 零基础CSS教程(一)
  8. GVINS学习记录1-消息订阅
  9. android遥控器控制播放器,android tv盒子播放器控制 监听上下左右键。
  10. 群晖218和218+区别_希捷企业级硬盘和希捷酷狼硬盘有什么区别