参考文献: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函数/迭代器的更多相关文章

  1. JavaScript中map函数和filter的简单举例(转)

    js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...

  2. map函数(转)

    STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.Map概述 Map是ST ...

  3. map函数用法详解

    map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...

  4. python中的zip()函数和map()函数

    一.zip()函数 1.语法: zip(iterable, ...) 参数说明: iterable,...-- 一个或多个迭代器; 在python2中: zip() 函数用于将可迭代的对象作为参数,将 ...

  5. Python 特殊函数解析(lambda 函数,map 函数,filter 函数,reduce 函数)

    写在之前 今天给大家介绍几个比较特殊的函数,他们具有函数式编程的特点,有人将它们视为 Python 可进行 「函数式编程」 的见证,至于什么是函数式编程,不是本篇文章的重点,感兴趣的可以去了解一下.老 ...

  6. 1.python函数式编程-map函数

    编程方法论 面向过程 函数式 面向对象 面向过程 将编程过程拆分成多个步骤,在函数中按照每个步骤进行编程: 函数式编程 编程语言定义的函数+数学意义的函数 1.不可变,不用变量保存状态,不修改变量: ...

  7. python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)

    1.用我们之前学过的求一下平方(只有一个列表) #求平方 num=[1,5,6,2,7,8] a=[] for n in num: a.append(n**2) print (a) C:\python ...

  8. python map函数的使用

    python2 中的map函数返回列表 python3 中的map函数返回迭代器 >>>def square(x) : # 计算平方数 ... return x ** 2 ... & ...

  9. JavaScript中map函数和filter的简单举例

    JavaScript的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似 1> filter是满足条件的留下,是对原数组的过滤:2> map则 ...

随机推荐

  1. Redis键管理

    Redis键管理 Redis 键命令用于管理 redis 的键. 语法 Redis 键命令的基本语法如下: redis > COMMAND KEY_NAME redis > SET w3c ...

  2. Strassen优化矩阵乘法(复杂度O(n^lg7))

    按照算法导论写的 还没有测试复杂度到底怎么样 不过这个真的很卡内存,挖个坑,以后写空间优化 还有Matthew Anderson, Siddharth Barman写了一个关于矩阵乘法的论文 < ...

  3. [洛谷P4389]付公主的背包

    题目大意:有$n(n\leqslant10^5)$种物品,第$i$个物品体积为$v_i$,都有$10^5$件.给定$m(m\leqslant10^5)$,对于$s\in [1,m]$,请你回答用这些商 ...

  4. 太空飞船(spaceship)

    太空飞船(spaceship) 题目描述 21XX年,秋. 小诚是THU(Tomorrow Happy University)航天学院船舶设计系本科四年级的学生.为了顺利毕业,小诚仔细阅读了这几年被引 ...

  5. HDU 4649 Professor Tian (概率DP)

    Professor Tian Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  6. 解决_CRT_SECURE_NO_WARNINGS 警告

    问题:我们在程序中使用fopen等CRT函数,就会出现一些警告信息,很烦人,如下: 1>e:/project/htt/ishow/functions.cpp(156) : warning C49 ...

  7. matlab-bgl 工具包配置环境 TDM-GCC-64 mex

    Windows 64 + Matlab 64 MEX混合编程初步: http://blog.csdn.net/enjoyyl/article/details/46545263 使用的是 TDM-GCC ...

  8. 11.OpenStack 安装监控和业务流程服务

    安装业务流程模块 安装和配置业务流程 创建数据库 mysql -uroot -ptoyo123 CREATE DATABASE heat; GRANT ALL PRIVILEGES ON heat.* ...

  9. mysq 中 information_schema 库

    information_schema这个库,这个在mysql安装时就有了,提供了访问数据库元数据的方式.那什么是元数据库呢?元数据是关于数据的数据,如数据库名或表名,列的数据类型,或访问权限等.有些时 ...

  10. UVA 1594:Ducci Sequence (模拟 Grade E)

    题意: 对于一个n元组(a0,a1,...),一次变换后变成(|a0-a1|,|a1-a2|,...) 问1000次变换以内是否存在循环. 思路: 模拟,map判重 代码: #include < ...