向map添加元素:

因为map是不允许出现重复关键字的,所以如果重复插入键相同的元素后面的元素是不会插入成功的,下面是一个验证程序:

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std; int main()
{
map<string, int> mmap;
mmap.insert({ "wu",1 });
mmap.insert({ "xiao",1});
mmap.insert({ "wu",2 });
mmap.insert({ "xiao",8 });
for (auto it : mmap)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

  运行结果:

从运行结果我们可以知道,mmap的第三条个第四条插入语句时没法插入成功的,因为前面已经对相同键值做过了插入操作,后面就不会再插入了。

如果想要四条语句都插入成功可以考虑用multimap,multimap是可以存在重复键值的,下面是验证程序

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std; int main()
{
multimap<string, int> mmap;
mmap.insert({ "wu",1 });
mmap.insert({ "xiao",1});
mmap.insert({ "wu",2 });
mmap.insert({ "xiao",8 });
for (auto it : mmap)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

  运行结果:

map容器最常用的方法——kv对计数,如果插入的元素还没存在就插入,并给value赋值为1,如果插入的元素已经存在就不再插入而是给对应的键的值加1

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
using namespace std; int main()
{
map<string, int> mmap;
string str;
while (cin >> str)
{
mmap[str]++;
}
for (auto it : mmap)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

  运行结果:

map的各种插入数据方式:

#include <iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{ map<string, int>mmap;
mmap.insert(pair<string,int>("fsdfads", 43));//第一种插入方式
mmap.insert(map<string, int>::value_type("fsdf", 5));//第二种
mmap["fsdff"] = 3;//第三种
mmap.insert({ "fsd",4 });//第四种
for (auto it : mmap)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

  运行结果:

对map中的value进行排序
#include <iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; int main()
{ map<string, int>mmap;
vector<pair<string, int>>vec;
mmap.insert(pair<string,int>("fsdfads", 43));
mmap.insert(map<string, int>::value_type("fsdf", 5));
mmap["fsdff"] = 3;
mmap.insert({ "fsd",4 });
//将map的key和value以pair的形式装到vector中,对vector进行排序。
for (auto it = mmap.begin(); it != mmap.end(); it++)
{
vec.push_back(make_pair(it->first, it->second));
}
sort(vec.begin(), vec.end(), [](const pair<string, int>&x, const pair<string, int>&y) {return x.second < y.second; });
for (auto it : vec)
{
cout << it.first << " " << it.second << endl;
}
return 0;
}

  运行结果:

#include <iostream>
#include <cstdlib>
#include <map>
#include <vector>
#include <string>
#include <algorithm> using namespace std; int cmp(const pair<string, int>& x, const pair<string, int>& y)
{
return x.second > y.second;
} void sortMapByValue(map<string, int>& tMap, vector<pair<string, int> >& tVector)
{
for (map<string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)
tVector.push_back(make_pair(curr->first, curr->second)); sort(tVector.begin(), tVector.end(), cmp);
}
int main()
{
map<string, int> tMap;
string word;
while (cin >> word)
{
pair<map<string, int>::iterator, bool> ret = tMap.insert(make_pair(word, 1));
if (!ret.second)
++ret.first->second;
} vector<pair<string, int>> tVector;
sortMapByValue(tMap, tVector);
for (int i = 0; i < tVector.size(); i++)
cout << tVector[i].first << ": " << tVector[i].second << endl; system("pause");
return 0;
}

  

map的基本操作的更多相关文章

  1. C++ map的基本操作和使用

    原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...

  2. c++ map 的基本操作

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  3. C++中map的基本操作和使用;

    注:本文来自sina live 的博文 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本 ...

  4. 【转】 C++ map的基本操作和使用

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

  5. 转载:C++ map的基本操作和使用

    声明:本文转自:http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html 1.map简介 map是一类关联式容器.它的特点是增 ...

  6. map的基本操作函数及含义

    map的基本操作函数:      C++ Maps是一种关联式容器,包含“关键字/值”对      begin()          返回指向map头部的迭代器      clear()        ...

  7. cocos2d-x3.2中map的基本操作和使用

    在游戏开发中,我们有时候会用到map,而map的使用方法我简单给大家介绍一下.Map是c++的一个标准容器,她提供了非常好一对一的关系,在一些程序中建立一个map能够起到事半功倍的效果,总结了一些ma ...

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

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

  9. 【转】C++ map的基本操作和使用

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

  10. C++STL之map的基本操作

    STL中基本的关联式容器有map和set,它们都是以红黑树作为其底层的结构,具有非常高的查找.删除效率,内容会按照键值自动排序. 使用map的注意事项: 1.关联式容器的键值是不允许修改的,所以永远不 ...

随机推荐

  1. POJ 3041 Asteroids(二分图最大匹配)

    ###题目链接### 题目大意: 给你 N 和 K ,在一个 N * N 个图上有 K 个 小行星.有一个可以横着切或竖着切的武器,问最少切多少次,所有行星都会被毁灭. 分析: 将 1~n 行数加入左 ...

  2. C# 中的浅拷贝与深拷贝

    Ø  简介 在 C# 中分为两种数据类型,值类型和引用类型.我们知道,值类型之间赋值是直接将值赋值给另一个变量,两个变量值的改变都互不影响:而引用类型赋值则是将引用赋值给另一个变量,其中一个变量中的成 ...

  3. 云原生生态周报 Vol.10 | 数据库能否运行在 K8s 当中?

    业界要闻  IBM 以总价 340 亿美元完成里程碑意义的红帽收购:这是这家拥有 107 年历史的公司史上规模最大的一笔收购,该收购金额在整个科技行业的并购史上也能排到前三.在当天公布的声明中,IBM ...

  4. 钉钉SDK使用。

    (1)到 https://open-doc.dingtalk.com/microapp/faquestions/vzbp02 下载SDK (2)引入 using DingTalk.Api; using ...

  5. Python笔记:threading(多线程操作)

    Python的线程操作在旧版本中使用的是thread模块,在Python27和Python3中引入了threading模块,同时thread模块在Python3中改名为_thread模块,thread ...

  6. Appium基于PO模型的自动化测试(Python)

    基于python单元测试框架unittest完成appium自动化测试,生成基于html可视化测试报告 代码示例: #利用unittest并生成测试报告 class Appium_test(unitt ...

  7. vue中路由传值url--路径传值

    在vue项目中我们使用路径的方式一般有一下两种方式this.$route.params.userId;一种需要在router上配置对应的数据key, this.$route.query.userId;

  8. ca动画

    //动画上下文-(void)animationOfUIKit{    UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 1 ...

  9. 【web后端开发】笔试题收集

    4399Web后端开发笔试题 题目来源:牛客网 1.linux中,用mkdir命令创建新的目录时,如果需要在其父目录不存在时先创建父目录的选项是   D A  -h B -d C  -f D -p [ ...

  10. 数据库系统(五)---MySQL基础

    一.SQL基本概念: SQL 已经成为关系数据库的标准语言,是一种数据库查询和程序设计语言,用 于存取数据以及查询.更新和管理关系数据库系统. 功能不仅仅是查询,还包括数据定义.数据操纵和数据控制等于 ...