map使用
// map使用
1 #include <iostream>
#include "insertVal.h"
#include "sort.h"
using namespace std; void main()
{
////////////三种插入方式////////////
InsertVal insert;
insert.insert_1();
cout<<"-------------------"<<endl;
insert.insert_2();
cout<<"-------------------"<<endl;
insert.insert_3();
cout<<"-------------------"<<endl;
/*以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,
当然了第一种和第二种在效果上是完全一样的,用insert函数插入数据,
在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,
insert操作是插入不了数据的,但是用数组方式就不同了,它可以覆盖以
前该关键字对应的值。*/ //////////map的大小//////////
map<int, int> iMap;
iMap[] = ;
iMap[] = ;
iMap[] = ;
cout<<iMap.size()<<endl; //////////map的遍历//////////
//
map<int, int>::reverse_iterator riter;
for(riter = iMap.rbegin(); riter != iMap.rend(); ++riter)//这里不要用后加,因为iterator是类模板,后加要返回一个无用的临时对象,
//而it++是函数重载,所以编译器无法对其进行优化,所以每遍历一个元素,你就创建并销毁了一个无用的临时对象。
{
cout<<riter->first<<" "<<riter->second<<endl;
}
//
map<int, int>::iterator iter;
for(iter = iMap.begin(); iter != iMap.end(); ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//
map<string, int> sMap;
sMap["age"] = ;
sMap["height"] = ;
sMap["weight"] = ;
string str[] = {"age", "height", "weight"};
for (int i=; i<sMap.size(); i++)
{
auto val = sMap.at(str[i]);
cout<<str[i]<<":"<<val<<endl;
} //用count函数来判定关键字是否出现,结果返回1/0
cout<<sMap.count("age")<<endl; //用find函数来定位数据出现位置,它返回的一个迭代器,
//当数据出现时,它返回数据所在位置的迭代器,如果map
//中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
auto iters = sMap.find("age");
if(iters != sMap.end())
cout<<iters->first<<":"<<iters->second<<endl;
else
cout<<"no find!"<<endl; //Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
//Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
//例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,
//而upper-bound(2)的话,返回的就是3
//Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,
//pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,
//则说明map中不出现这个关键字,
auto iter_1 = sMap.lower_bound("b");
auto iter_2 = sMap.upper_bound("b");
auto iter_pair = sMap.equal_range("ab"); /////////erase
sMap.erase("age");
sMap.erase(sMap.find("height"));
sMap.erase(sMap.begin(), --sMap.end());//删除一个区域是前闭后开的区间 /////////
if(sMap.empty())
sMap.clear(); sortShow();
return;
}
其中insertVal.h:
#ifndef INSERTVAL_H
#define INSERTVAL_H #include <iostream>
#include <map>
#include <string>
using namespace std; class InsertVal
{
public:
void insert_1();
void insert_2();
void insert_3();
}; void InsertVal::insert_1()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, "Mical"));
mapStudent.insert(pair<int, string>(, "Meria"));
mapStudent.insert(pair<int, string>(, "Sam"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} void InsertVal::insert_2()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (, "Mical"));
mapStudent.insert(map<int, string>::value_type (, "Meria"));
mapStudent.insert(map<int, string>::value_type (, "Sam"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} void InsertVal::insert_3()
{
map<int, string> mapStudent;
mapStudent[] = "Mical";
mapStudent[] = "Meria";
mapStudent[] = "Sam";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} #endif
sort.h:
#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <map>
#include <string>
using namespace std;
/*
map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的
STL中默认是采用小于号来排序,
在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,
因为它没有小于号操作,insert等函数在编译的时候过不去
方法一:重载小于号。略
方法二:仿函数的应用,这个时候结构体中没有直接的小于号重载。如下
*/ typedef struct tagStudentInfo
{
int nID;
string strName;
}StudentInfo, *PStudentInfo; //学生信息 class sort
{
public:
bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
{
if(_A.nID < _B.nID) return true;
if(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < ;
return false;
}
}; void sortShow()
{
//用学生信息映射分数
map<StudentInfo, int, sort>mapStudent;
StudentInfo studentInfo;
studentInfo.nID = ;
studentInfo.strName = "student_one";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
studentInfo.nID = ;
studentInfo.strName = "student_two";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
} #endif
map使用的更多相关文章
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- 多用多学之Java中的Set,List,Map
很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析
在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- MapReduce剖析笔记之五:Map与Reduce任务分配过程
在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...
- MapReduce剖析笔记之三:Job的Map/Reduce Task初始化
上一节分析了Job由JobClient提交到JobTracker的流程,利用RPC机制,JobTracker接收到Job ID和Job所在HDFS的目录,够早了JobInProgress对象,丢入队列 ...
随机推荐
- 【sqli-labs】 less14 POST - Double Injection - Single quotes- String -twist (POST型单引号变形双注入)
名字和less13一样? 看了下源码对比 less13 less14 less14应该是双引号吧 出错 构造永真登陆
- wx小程序开发 1:小程序代码构成
官网学习地址:https://developers.weixin.qq.com/miniprogram/dev/quickstart/basic/introduction.html 1: 2:待续.. ...
- 不能访问windows installer 服务,可能你在安全模式下运行 windows ,或者windows installer
windows installer服务解决方案 很多朋友在安装MSI格式的文件包时,经常会遇到windows installer出错的情况,有如下几种现象: 1.所有使用windows install ...
- Docker 导入镜像报错:open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no such file or directory
下载好了redis的tar包,然后导入redis镜像是报错open /var/lib/docker/tmp/docker-import-743441288/redis-3.0.7/json: no s ...
- LINUX - getopts
getopts optionString opt; optionString :所有参数组成的-参数串: opt:从optionString 每次取的参数值: 当optionString用[:]开头, ...
- TensorFlow学习笔记----例子(2)
使用TensorFlow中的梯度下降法构建线性学习模型的使用示例:(来源:https://morvanzhou.github.io/tutorials/machine-learning/tensorf ...
- Leetcode 动态规划 - 简单
1. 最大子序和 (53) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输 ...
- [51Nod1446] 限制价值树 (容斥+MT定理+折半搜索)
传送门 Description 有N个点(N<=40)标记为0,1,2,...N-1,每个点i有个价值val[i],如果val[i]=-1那么这个点被定义为bad,否则如果val[i] > ...
- python 用PIL Matplotlib处理图像的基本操作
在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片.本人偏爱 matpoltlib,因为它的语法更像 matlab. 一.matplotlib 1. ...
- 【Codeforces Round #519 by Botan Investments D】Mysterious Crime
[链接] 我是链接,点我呀:) [题意] 相当于问你这m个数组的任意长度公共子串的个数 [题解] 枚举第1个数组以i为起点的子串. 假设i..j是以i开头的子串能匹配的最长的长度. (这个j可以给2. ...