c++之map函数/迭代器
参考文献:https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html
#include <iostream>
#include <map>
#include <string>
using namespace std; int main()
{
//删除某个元素 erase():
map<int, string> mapStu;
mapStu.insert(map<int, string>::value_type(, "stu1"));
mapStu.insert(map<int, string>::value_type(, "stu2"));
mapStu.insert(pair<int, string>(, "stu3")); map<int, string>::iterator itor;
itor = mapStu.find(); mapStu.erase(); //根据键值删除某个元素
//mapStu.erase(itor); //根据迭代器删除
//mapStu.erase(mapStu.begin(), mapStu.end()); //删除一个范围内的 for (itor = mapStu.begin(); itor != mapStu.end(); itor++)
cout << itor->second <<endl; //find函数:传入的参数是要查找的key。
//用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,
//它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
/* map<int, string> mapStu;
mapStu.insert(map<int, string>::value_type(1, "stu1"));
mapStu.insert(map<int, string>::value_type(2, "stu2"));
mapStu.insert(pair<int, string>(3, "stu3")); map<int, string>::iterator itor;
itor = mapStu.find(3);
if (itor != mapStu.end())
cout << itor->second << endl; //stu3 itor = mapStu.lower_bound(1);
if (itor != mapStu.end())
cout << itor->second << endl; //stu1 itor = mapStu.upper_bound(1);
if (itor != mapStu.end())
cout << itor->second << endl; //stu2 itor = mapStu.lower_bound(2);
if (itor != mapStu.end())
cout << itor->second << endl; //stu2 itor = mapStu.upper_bound(2);
if (itor != mapStu.end())
cout << itor->second << endl; //stu3 pair<map<int, string>, map<int, string>> pairStu;
pairStu = mapStu.equal_range(2); //equal_range(2)
if (pairStu.first == pairStu.second)
cout << "不存在" << endl;
*/
//数组形式遍历
/*map<int, string> mapStu;
mapStu.insert(map<int, string>::value_type(1, "stu1"));
mapStu.insert(map<int, string>::value_type(2, "stu2"));
mapStu.insert(pair<int, string>(3, "stu3")); int nsize = mapStu.size();
for (int size = 1; size <= nsize; size++)
cout << mapStu[size] << endl;*/ //反向遍历
//map<int, string> mapStu;
//mapStu.insert(map<int, string>::value_type(1, "stu1"));
//mapStu.insert(map<int, string>::value_type(2, "stu2"));
//mapStu.insert(pair<int, string>(3, "stu3"));
//pair<map<int, string>::iterator, bool> pair_insert; //pair_insert = mapStu.insert(pair<int, string>(3, "stu4"));
//if (pair_insert.second == true)
// cout << "success" << endl;
//else
// cout << "fail" << endl; //map<int, string>::reverse_iterator itor;
//for (itor = mapStu.rbegin(); itor != mapStu.rend(); itor++)
// cout << itor->first << " " << itor->second << endl; //int size = mapStu.size();
//cout << size << endl; //关于是否能够替换:数组方式2
//map<int, string> mapStu;
//mapStu.insert(map<int, string>::value_type(1, "stu1"));
//mapStu.insert(map<int, string>::value_type(2, "stu2"));
//mapStu.insert(pair<int, string>(3, "stu3"));
//pair<map<int, string>::iterator, bool> pair_insert; //pair_insert = mapStu.insert(pair<int, string>(3, "stu4"));
//if (pair_insert.second == true)
// cout << "success" << endl;
//else
// cout << "fail" << endl; //map<int, string>::iterator itor;
//for (itor = mapStu.begin(); itor != mapStu.end(); itor++)
// cout << itor->first << " " << itor->second <<endl; //int size = mapStu.size();
//cout << size << endl; //关于是否能够替换:数组方式1
//map<int, string> mapStu;
//mapStu[1] = "stu0";
//mapStu[2] = "stu1";
//mapStu[3] = "stu2";
//mapStu[3] = "stu3"; //输出替换了stu2
//map<int, string>::iterator itor;
//for (itor = mapStu.begin(); itor != mapStu.end(); itor++)
// cout << itor->second << endl; //方法3
//map<int, string> mapStu;
//mapStu[1] = "stu0";
//mapStu[2] = "stu1";
//mapStu[3] = "stu2";
//map<int, string>::iterator itor;
//for (itor = mapStu.begin(); itor != mapStu.end(); itor++)
// cout << itor->first << endl; //方法2
// map<int, string> mapStu;
// mapStu.insert(map<int,string>::value_type(1, "stu1"));
// mapStu.insert(map<int,string>::value_type(2, "stu2"));
// mapStu.insert(map<int, string>::value_type(3, "stu3"));
// map<int, string>::iterator iter;
// for (iter = mapStu.begin(); iter != mapStu.end(); iter++)
// cout << iter->second << endl; //方法1
/*map<int, string> mapStu;
mapStu.insert(pair<int, string>(1, "stu1"));
mapStu.insert(pair<int, string>(2, "stu2"));
mapStu.insert(pair<int, string>(3, "stu3")); map<int, string>::iterator inter;
for (inter = mapStu.begin(); inter != mapStu.end(); inter++)
cout << inter->first << endl;*/ system("pause");
return ;
}
c++之map函数/迭代器的更多相关文章
- JavaScript中map函数和filter的简单举例(转)
js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...
- map函数(转)
STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.Map概述 Map是ST ...
- map函数用法详解
map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...
- python中的zip()函数和map()函数
一.zip()函数 1.语法: zip(iterable, ...) 参数说明: iterable,...-- 一个或多个迭代器; 在python2中: zip() 函数用于将可迭代的对象作为参数,将 ...
- Python 特殊函数解析(lambda 函数,map 函数,filter 函数,reduce 函数)
写在之前 今天给大家介绍几个比较特殊的函数,他们具有函数式编程的特点,有人将它们视为 Python 可进行 「函数式编程」 的见证,至于什么是函数式编程,不是本篇文章的重点,感兴趣的可以去了解一下.老 ...
- 1.python函数式编程-map函数
编程方法论 面向过程 函数式 面向对象 面向过程 将编程过程拆分成多个步骤,在函数中按照每个步骤进行编程: 函数式编程 编程语言定义的函数+数学意义的函数 1.不可变,不用变量保存状态,不修改变量: ...
- python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)
1.用我们之前学过的求一下平方(只有一个列表) #求平方 num=[1,5,6,2,7,8] a=[] for n in num: a.append(n**2) print (a) C:\python ...
- python map函数的使用
python2 中的map函数返回列表 python3 中的map函数返回迭代器 >>>def square(x) : # 计算平方数 ... return x ** 2 ... & ...
- JavaScript中map函数和filter的简单举例
JavaScript的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似 1> filter是满足条件的留下,是对原数组的过滤:2> map则 ...
随机推荐
- 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...
- C++中include<> 与 include" " 的区别
<>时先去系统目录中找头文件,如果没有再到当前目录下找.所以像标准的头文件 stdio.h, stdlib.h等都用<>; ""则首先到当前目录下找,如果找 ...
- [洛谷P3321][SDOI2015]序列统计
题目大意:给你一个集合$n,m,x,S(S_i\in(0,m],m\leqslant 8000,m\in \rm{prime},n\leqslant10^9)$,求一个长度为$n$的序列$Q$,满足$ ...
- Web实现数据库链接的登录注册修改密码功能
/** * Copyright (C), 2017-2017 * FileName: User * Author: ichimoku * Date: 2017/12/5 14:31 * version ...
- C#实战Microsoft Messaging Queue(MSMQ)消息队列
前言 在使用MSMQ之前,我们需要自行安装消息队列组件!(具体安装方法大家自己搜一下吧) 采用MSMQ带来的好处是:由于是异步通信,无论是发送方还是接收方都不用等待对方返回成功消息,就可以执行余下的代 ...
- poj~3694Network
Description A network administrator manages a large network. The network consists of N computers and ...
- 今天写页面,将.net也使用了语义法规来判断。做法来源是.net ado.net的类型判断 Infertype。一样的原理
做了一个封装,使页面更容易维护一些.主要是两个方法,如下所示: private void BindModule(Action<Repeater, string> bindSingRpt) ...
- Promotion Counting(USACO)
题目翻译: zxyer来到了一个神奇的公司工作,之所以神奇,是因为这个公司的员工的职位并不与他们的水平相称,有的职位极低的职员的经验非常丰富,而有些经理甚至老板都是个萌新.有一天,zxyer收到了老板 ...
- CRichEditCtrl 输入字符串长度限制
1.我用 CRichEditCtrl 控件,发现它通过代码可以向里面写大于 32KB 的字符,但手工却只能输入小于 32767 个字符,再多则自动舍弃. 2.初始化时调用CRichEditCtrl:: ...
- ANDROID开发笔记(二)
动机: 开发的一个背单词的软件. 不会实现划屏的特性. 方法: 第一步尝试: 在MainActivity中, 增加以下代码后, 如果在视图的空白处点击时, 文本框中的时间就会发生改变. @Overri ...