c++中stl----map
1 map的本质
(1)关联式容器,键值对应
(2)增加和删除节点对迭代器的影响很小。
(3)对于迭代器来说不可以修改键值,只能修改对应的实值。
(4)map内部数据的祖居是自建一颗红黑树(或者说是平衡二叉树),具有自动排序的功能。
2 map的查增删
(1)map的插入
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
//方式一 pair
map<int,string> mapStudent;
mapStudent.insert(pair<int,string>(1,"lan"));
mapStudent.insert(pair<int,string>(2,"ji"));
mapStudent.insert(pair<int,string>(1,"kjh"));
map<int,string>::iterator iter;
map<int,string>::iterator iter1;
//方式二
map<int string> mapStudent1;
mapStudent1.insert(map<int,string>::value_type(1,"nihao"));
mapStudent1.insert(map<int,string>::value_type(1,"ben"));
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}
2 map的遍历
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.find(1);
if(iter!=mapStudent.end())
{
cout<<"找到了 value="<<iter->second<<endl;
}else
{
cout<<"没有找到"<<endl;
}
return 0;
}
3 map的删除
#include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.begin();
for(;iter!=mapStudent.end();)
{
if((*iter).sencond=="stu_one")
{
mapStudent.erase(iter++);//iter被erase以后就会失效 所以后面不能再有iter++
}else
{
++iter;
}
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}
4 map排序
默认按照key从小到大。从大到小greater 相反less
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string,int,greater<string>> mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
自定义排序方式
#include <string>
#include <iostream>
using namespace std;
struct CmpByKeyLength
{
bool operator()(const string &k1,const string&k2){
return k1.length()<k2.length();
}
};
int main()
{
map<string,int,CmpByKeyLength>mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}
------>加油美好的一天
c++中stl----map的更多相关文章
- STL MAP及字典树在关键字统计中的性能分析
转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...
- STL中关于map和set的四个问题?
STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...
- STL 中的map 与 hash_map的理解
可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...
- STL中的map和unordered_map
STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...
- STL中的map和hash_map
以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...
- stl::map之const函数访问
如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...
- hdu4941 Magical Forest (stl map)
2014多校7最水的题 Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- STL map 用法
首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象 class pair可以将两个值视为一个单元.容器类别map和mul ...
随机推荐
- 多线程(C++)临界区Critical Sections
一 .Critical Sections(功能与Mutex相同,保证某一时刻只有一个线程能够访问共享资源,但是不是内核对象,所以访问速度比Mutex快,但是没有等待超时的功能,所以有可能导致死锁,使用 ...
- 在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
- 10个必需的iOS开发工具和资源
本文转载至 http://mobile.51cto.com/iphone-418166.htm 界面总不是一件很容易事,尤其是iPhone/iPad的界面,做过iOS开发的程序员,一定会感到开发iPh ...
- 基于EasyDarwin云平台实现的EasyClient客户端与EasyCamera摄像机之间的对讲与云台控制功能
本文转自EasyDarwin团队Kim的博客,感谢Kim长期对EasyDarwin开源项目的贡献:http://blog.csdn.net/jinlong0603 EasyDarwin云平台是一套由E ...
- 九度OJ 1097:取中值 (中值)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5092 解决:1411 题目描述: 存在两组数组,和4个数字a,b,c,d,要求做如下操作,将第一个数组第a个数到第b个数,第二个数组的第c ...
- webapp 打包
输入您的WAP网址,技术员马上帮您封装APP! APP人工打包-智睿软件_app打包_苹果app发布_app 上架_ios 上架_封装app_网站转app_安卓发布 http://app.niuhu1 ...
- 【智能无线小车系列九】在树莓派上使用USB摄像头
材料准备: 1.树莓派 2.AS 4WD小车 3.WebCam 4.小米移动电源 5.TP—LINK 高增益150MUSB无线网卡 操作流程: 1.将WebCam插上树莓派后,首先要确认树莓派是否支持 ...
- Vue数据双向绑定探究
前面的啰嗦话,写一点吧,或许就有点用呢 使用过vue的小伙伴都会感觉,哇,这个框架对开发者这么友好,简直都要笑出声了. 确实,使用过vue的框架做开发的人都会感觉到,以前写一大堆操作dom,bom的东 ...
- 前端面试常考知识点---CSS
前端面试常考知识点---js 1.CSS3的新特性有哪些 点我查看 CSS3选择器 . CSS3边框与圆角 CSS3圆角border-radius:属性值由两个参数值构成: value1 / valu ...
- POJ2389 —— 高精度乘法
直接上代码了: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib& ...