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则 ...
随机推荐
- ACM童年生活二三事
描述 Redraiment小时候走路喜欢蹦蹦跳跳,他最喜欢在楼梯上跳来跳去. 但年幼的他一次只能走上一阶或者一下子蹦上两阶. 现在一共有N阶台阶,请你计算一下Redraiment从第0阶到第N阶共有几 ...
- 每天一个小算法(Shell Sort1)
希尔排序是1959 年由D.L.Shell 提出来的,相对直接排序有较大的改进.希尔排序又叫缩小增量排序 基本思想: 先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录 ...
- Linux命令之rhn_check
NAME rhn_check - check for queued actions on RHN and execute them SYNOPSIS /usr/sbin/rhn_check [-v] ...
- 图表绘制工具--Matplotlib 1
''' [课程3.] Matplotlib简介及图表窗口 Matplotlib → 一个python版的matlab绘图接口,以2D为主,支持python.numpy.pandas基本数据结构,运营高 ...
- 洛谷P3112 [USACO14DEC]后卫马克Guard Mark
题目描述 Farmer John and his herd are playing frisbee. Bessie throws the frisbee down the field, but it' ...
- .net使用memcached
Windows中memached安装 -------------服务器端配置 1>开始>运行:CMD(确定) 2>cd C:\memcached(回车) 3>memcached ...
- C#操作XML序列化与反序列化
public class XmlSerializerHelper { /// <summary> /// 从XML文件中反序列化读取对象 /// </summary> /// ...
- PHP命令行执行程序php.exe使用及常用参数
PHP命令行执行程序php.exe参数说明 -f <file>:以命令行方式运行指定的PHP文件,只要指定具体的PHP文件(带绝对路径),php.exe就可以执行PHP文件,所以这个参数单 ...
- suse10与suse11连接Xmanager的配置
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://feidandelion.blog.51cto.com/1050439/42217 ...
- Object和Thread自带的原生方法
Object类: 1) clone():创建并返回此对象的一个副本. 2) equals(obj):指示其对象是否与此对象“相等”. 3) finalize():当垃圾回收器确定不存在对该对象的更多引 ...