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则 ...
随机推荐
- hdu 1142 最短路+记忆化
最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...
- 淀粉质模板 Tree
Tree 题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入输出格式 输入格式: N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 ...
- Leetcode 4. Median of Two Sorted Arrays(二分)
4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...
- 搭建自己的YUM源HTTP服务器
createrepo是linux下的创建仓库的软件包.create是创建的意思,repo是repository的缩写,是仓库的意思.yum(Yellowdog Updater,Modified)主要的 ...
- UIImage 获取url图片资源
//UIImage 获取url图片资源 -(UIImage *) getImageFromURL:(NSString *)fileURL { UIImage * result; NSData * da ...
- .NET 4.0中使用sqlite
原文发布时间为:2011-12-10 -- 来源于本人的百度文章 [由搬家工具导入] http://www.csharpcity.com/sqlite-ado-net-c-4-0/ Getting S ...
- 我们曾经心碎的C#之 第一章.我的第一个C#程序
第一章. C#入门 1.1 .NET与C# 001..NET是Microsoft.NET的简称,是基于Windows平台的一种技术 ...
- centos 安装squid http代理
yum -y install squid service squid start service iptables stop
- Android控件介绍
1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...
- picker(拖拽上下拉动的选项)
[b]新版本更新:鼠标上下拖动选择内容:http://hiuman.iteye.com/blog/2353563[/b] (如有错敬请指点,以下是我工作中遇到并且解决的问题) 一开始搜这个内容的时候, ...