map的基本操作
向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的基本操作的更多相关文章
- C++ map的基本操作和使用
原文地址:http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可 ...
- c++ map 的基本操作
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ...
- C++中map的基本操作和使用;
注:本文来自sina live 的博文 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本 ...
- 【转】 C++ map的基本操作和使用
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...
- 转载:C++ map的基本操作和使用
声明:本文转自:http://www.cnblogs.com/hailexuexi/archive/2012/04/10/2440209.html 1.map简介 map是一类关联式容器.它的特点是增 ...
- map的基本操作函数及含义
map的基本操作函数: C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() ...
- cocos2d-x3.2中map的基本操作和使用
在游戏开发中,我们有时候会用到map,而map的使用方法我简单给大家介绍一下.Map是c++的一个标准容器,她提供了非常好一对一的关系,在一些程序中建立一个map能够起到事半功倍的效果,总结了一些ma ...
- C++使用: C++中map的基本操作和用法
在阅读SSD代码中发现作者使用了C++中的map方法,因此搜索该关联式容器的使用方法,在这里一并总结. 一.Map 簡介 Map是STL的一個容器,它提供一對一的hash. 第一個可以稱為關鍵字(ke ...
- 【转】C++ map的基本操作和使用
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响较小,除了那个操作节点,对其它的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...
- C++STL之map的基本操作
STL中基本的关联式容器有map和set,它们都是以红黑树作为其底层的结构,具有非常高的查找.删除效率,内容会按照键值自动排序. 使用map的注意事项: 1.关联式容器的键值是不允许修改的,所以永远不 ...
随机推荐
- ES6中有关数组的一些新操作
1.Array.isArray() 用于确定传递的值是否是一个 Array. Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); ...
- spring boot 不占用端口方式启动
随着微服务架构的流行,想要启动一个微服务架构项目就要开启好多端口,有时候一台机器上部署的项目多的时候,端口资源就比较紧张了,其实有的微服务组件仅仅只是提供RPC服务,可以不用占用web启动的端口,此时 ...
- 普通的行专列;oracle行专列;更新中。。。
题记 本来想写一个完整的表创建,但是其他人都写过啦,要不这样,你们有什么行转列的问题给我留言,我直接回答如何 Oracle的行转列 这篇文章不错:https://blog.csdn.net/huay_ ...
- jQuery 源码分析(一) 代码结构
jQuery是一个Javascript库,它支持链式操作方式,即对发生在同一个JQuery对象上的一组动作,可以直接接连写无需要重复获取对象.这一特点使得JQuery的代码无比优雅,而且有强大的选择器 ...
- [CrackMe]160个CrackMe之002
吾爱破解专题汇总:[反汇编练习]160个CrackME索引目录1~160建议收藏备用 一.逆向分析之暴力破解 暴力破解,对于这种具有提示框的,很好定位. 定位好之后,爆破其跳转语句,就能达到破解目的. ...
- ElasticSearch简介(一)——基础
基本概念 1. Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例. 单个 Elastic 实例称为一个节点 ...
- efcore mysql数据库codefirst生成
添加引用 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Tools Pomelo.EntityFrameworkCore.My ...
- Python教程 | Requests的基本用法
下面我就给大家整理了Requests库的使用方法和细节. 什么是Requests Requests是Python语言编写,基于urllib3,采用Apache2 Licensed开源协议的HTTP库. ...
- Redis操作篇(二)
redis的发布与订阅,主从架构,哨兵架构,cluster集群 下载编译安装redis # 1. 下载redis wget http://download.redis.io/releases/redi ...
- page的js访问全局变量:app.globalData.openid
page获取app.js:const app = getApp(); page的js访问全局变量(get/set):const app = getApp(); app.globalData.openi ...