stl的各种用法
3.遍历
(1). for(vector<datatype>::iterator it=a.begin(); it!=a.end();it++)
cout<<*it<<endl;
(2). for(int i=0;i<a.size;i++)
cout<<a[i]<<endl;
二. map
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性 map内部的实现自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能。
下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,
很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),
下面给出map描述代码:
1. 声明方式:
Map<int, string> mapStudent;
2. 数据的插入
在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法: 第一种:用insert函数插入pair数据
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
第二种:用insert函数插入value_type数据
Map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
第三种:用数组方式插入数据
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
3. map的大小
在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数: Int nSize = mapStudent.size();
4. 数据的遍历
第一种:应用前向迭代器
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
Cout<<iter->first<<” ”<<iter->second<<end;
第二种:应用反相迭代器
map<int, string>::reverse_iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
Cout<<iter->first<<” ”<<iter->second<<end;
第三种:用数组方式
int nSize = mapStudent.size()
STL中的常用的vector,map,set,sort, list用法笔记_计算机软件及应用_IT/计算机_专业资料。STL 中的常用的 vector,map,set,sort, list 用法笔记 C++的标准模板库...
STL中的常用的vector,map,set,Sort用法_IT/计算机_专业资料。STL中的常用的vector,map,set,Sort用法C++的标准模板库(Standard Template Library,简称STL)是一个容器...
STL 中的常用的Vector Map Set Sort用法_IT/计算机_专业资料。STL 中的常用的Vector Map Set Sort用法STL 中的常用的 vector map set Sort 用法 vector,map s...
STL 中的常用的 vector,map,set,Sort 用法 C++的标准模板库(Standard Template Library,简称 STL)是一个容器和算法的类库。容器 往往包含同一类型的数据。STL 中...
STL中有vector,string,set,map,multimap和deque等容...例:对int型的vector?vCount做正序排序。 sort(...2014全国高考状元联手分享状元笔记 衡水中学文科学霸...
STL 中的常用的 vector,map,set,Sort 用法 分类: C/C++ STL 中的常用的 vector,map,set,Sort 用法 C++的标准模板库(Standard Template Library,简称 STL)是...
插入操作和删除操作上比向(vector) veSTL中set用法...《建筑工程管理与实务》笔记总结67份文档 九...STL中sort函数用法简介 暂无评价 2页 免费喜欢...
STL中string、vector、list、deque、set、map 的区别_IT/计算机_专业资料。STL...STL 中的常用的Vector M... 5页 免费喜欢此文档的还喜欢 c++容器使用经验总结...
STL练习_IT/计算机_专业资料。关于C++STL的细节目标: 练习掌握STL中的vector,list,set,map容器;掌握sort find方法 STL中的vector,list,set,map容器 sort、 方法。...
结构中的两个分量) 的模板类,每一个元素包含了一个排序键(sort key)和一个...STL 中的常用的Vector M... 5页 免费 STL_VC 39页 免费 学习STL map, STL...
热门文档