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则 ... 
随机推荐
- AJAX提交表单后要清空,否则再次提交原来的数据会认为重复提交,提交失败。使用ajaxSubmit 函数需要引入jquery.form.min.js 文件
			<script src="../../Scripts/js/jquery.form.min.js" type="text/javascript">& ... 
- VMware 密匙
			11.0版本 1F04Z-6D111-7Z029-AV0Q4-3AEH8 亲测可用 
- <转自原博客>  NOIP2008 传纸条
			小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是,他们可以 ... 
- IE6对!important单个的类是支持的
			"!important"是什么? 第一个,是设置样式的优先级,设了!important的样式的属性优先于id选择器和class选择器.,比如id为"Main"的 ... 
- js中字符串常规操作
			string对象属性: 1.length 获取字符串的长度,需要注意的是,js中中文每个汉字也只代表一个字符. var myName="xulinjun"; console.log ... 
- c# tcplistener 与 client通信 服务端 今天写一下
			using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ... 
- 今天做一个winform,想直接把窗体改成输出类库,其他地方直接调结果总提示不能注册组件,回来调度,可以,总结,windows还是直接用新建的类型项目,改容易出错
			如题, 对于winform程序,还是新建一个类库,这样,在类库里面可以添加窗体.这样可以提供其他程序集来调用里面的窗体 
- number(NOIP模拟赛Round 3)
			好吧,神奇的题目.. 原题传送门 这道题目,神奇的字符单调栈.. 首先预处理出1~n个数(大家都会.) 然后塞进字符串里,输出答案(水~) 然后.. 我们需要将所有的字符存入单调栈中,然后乱搞,就可以 ... 
- Linux内核驱动之延时---内核超时处理【转】
			转自:http://blog.chinaunix.net/uid-24219701-id-3288103.html 内核超时处理 jiffies 计数器 定时器中断由系统定时硬件以规律地间隔产生; 这 ... 
- java1.7集合源码阅读:ArrayBlockingQueue
			ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ... 
