STL的使用和背后数据结构
STL(Standard Template Library即,模板库)包括六个部分:容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)
vector
1、vector:连续存储
(1)头文件,#include<vector>
(2)创建vector对象,vector<int> vec;
(3)尾部插入元素,vec.push_back(a);
(4)使用下标访问元素,cout<<vec[0]<<endl;
(5)使用迭代访问元素
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
3 cout<<(*it)<<endl;
(6)插入元素,vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a
(7)删除元素,vec.erase(vec.begin()+2);删除第3个元素
vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始
(8)向量大小,vec.size();
(9)清空,vec.clear();
vector的元素不仅仅只限于int型,int、double、string、全局结构体等都可以。
#include<iostream>
#include<vector>
using namespace std; struct Student
{
int num;
double score;
double operator< (const Student &stu) const
{
if(stu.score>score)
return stu.score;
else
return score;
}
}; int main()
{
vector<Student> stu;
//student 1
Student stu_temp;
stu_temp.num = ;
stu_temp.score =9.9;
stu.push_back(stu_temp);
//student 2
Student stu_temp1;
stu_temp1.num = ;
stu_temp1.score =8.8;
stu.push_back(stu_temp1);
//student 3
Student stu_temp2;
stu_temp2.num = ;
stu_temp2.score =7.7;
stu.push_back(stu_temp2);
//print all the students
cout<<"the number of student:"<<stu.size()<<endl;
vector<Student>::iterator it;
for(it=stu.begin();it!=stu.end();it++)
cout<<"number:"<<(*it).num<<" score:"<<(*it).score<<endl;
//delete one student
stu.erase(stu.begin()+);
cout<<endl;
cout<<"the number of student:"<<stu.size()<<endl;
for(it=stu.begin();it!=stu.end();it++)
cout<<"number:"<<(*it).num<<" score:"<<(*it).score<<endl;
//print the better score
double _result = stu_temp<stu_temp1;
cout<<endl;
cout<<"the better score:"<<_result<<endl; return ;
}
vector_sample
string
2、string
平时最常用的一个,这里就不做过多说明了
map
3、map:关联容器,提供一对一的数据映射(关键字,值);数据结构为红黑树(RB-Tree)
关键字只能在map中出现一次;另外,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的;
(1)头文件,#include<map>;
(2)创建map对象,map<int,string> mapStudent;
(3)插入数据,
第一种:用insert函数插入pair数据
mapStudent.insert(pair<int,string>(,"Christal"));
mapStudent.insert(pair<int,string>(,"Carl"));
第二种:用insert函数插入value_type数据
mapStudent.insert(map<int,string>::value_type (,"Christal"));
mapStudent.insert(map<int,string>::value_type (,"Carl"));
第三种:用数组方式插入数据
1 mapStudent[] = "Christal";
2 mapStudent[] = "Carl";
输出均为:

如果用前两种方法插入数据,因为关键字是唯一的,所以当关键字已经存在的时候,再插入相同关键字的map是不成功的;而第三种用数组插入的方法是仍然可以的,会将原来的关键字所对应的值进行更改,相当于被覆盖掉了。
所以要想知道前两种方法的插入是否成功,应该用一个返回值来检验。
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
map<int,string> mapStudent;
pair<map<int,string>::iterator,bool> insert_pairl; //insert 1 and check
insert_pairl = mapStudent.insert(pair<int,string>(,"Christal"));
if(insert_pairl.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl; //insert 2 and check
insert_pairl = mapStudent.insert(pair<int,string>(,"Carl"));
if(insert_pairl.second == true)
cout<<"Insert Successfully"<<endl;
else
cout<<"Insert Failure"<<endl; //insert 3 and check
insert_pairl = mapStudent.insert(pair<int,string>(,"Jerry"));
if(insert_pairl.second == true)
cout<<"Insert Successfully"<<endl<<endl;
else
cout<<"Insert Failure"<<endl<<endl; //print
map<int,string>::iterator it;
for(it=mapStudent.begin();it!=mapStudent.end();it++)
cout<<(*it).first<<" "<<(*it).second<<endl; return ;
}
map_insert_check
正如上面所说,当要插入的关键字已经存在,是插入失败的,所以输出结果为:

而采用数组插入方式会直接覆盖
mapStudent[] = "Christal";
mapStudent[] = "Carl";
mapStudent[] = "Jerry";
输出结果为:

(4)数据的遍历,当然分为用迭代器遍历的方式和用数组遍历的方式,其中以迭代器遍历中又分为正向遍历和反向遍历,正向遍历就是我们所熟知的迭代器遍历方式,反向遍历如下:
map<int,string>::iterator it; //print
2 for(it=mapStudent.begin();it!=mapStudent.end();it++)
3 cout<<(*it).first<<" "<<(*it).second<<endl;
4
5 map<int,string>::reverse_iterator rit; //reverse print
6 for(rit=mapStudent.rbegin();rit!=mapStudent.rend();rit++)
7 cout<<(*rit).first<<" "<<(*rit).second<<endl;
输出结果为:

(5)查找数据,一是用count()函数查找,存在返回1,否者返回0;二是用find()函数来定位数据出现的位置;
find()函数返回一个迭代器,如果找到数据,则返回数据所在位置的迭代器;如果不存在,则返回值与end()函数的返回值相同;
1 map<int,string>::iterator _iter;
2 _iter = mapStudent.find();
if(_iter != mapStudent.end())
4 cout<<"Find Successfully"<<endl;
5 else
6 cout<<"Find Failure"<<endl;
(6)删除数据,clear()和erase()
清空map中的所有数据用clear()函数,判定map中是否有数据用empty()函数,为空返回true。
选择性的删除用erase()函数,可以实现三种方式的删除,
用迭代器删除:
1 map<int,string>::iterator _iter;
2 _iter = mapStudent.find();
3 mapStudent.erase(_iter);
用关键字删除:
int n = mapStudent.erase();
if(n == )
3 cout<<"Erase Successfully"<<endl;
else
5 cout<<"Erase Failure"<<endl;
用迭代器成片删除,删除区间是一个前闭后开[ )的集合:
1 mapStudent.erase(mapStudent.begin(),mapStudent.end());
set
4、set:用来存储同一数据类型的数据,内部每个元素都是唯一的,且自动排序;数据结构为红黑树(RB-Tree)
(1)构造函数,set<int> c;
(2)查找函数,find()函数和count()函数;
(3)数据访问函数,begin()、end()、rbegin()、rend();
(4)插入数据,insert(element)、insert(position,element)、insert(begin,end);
(5)删除数据,erase(position)、erase(element)、erase(begin,end);
hash_map&hash_set
5、hash_map和hash_set:底层数据结构是哈希表
hash_map与map用法类似,只是内部数据结构不同,hash_map提供内部数据随机、更快的访问;hash_set同理。
总结
6、总结:
(1)vector封装数组,list封装链表,map和set封装了二叉树;
(2)对于这些STL,应当掌握基本的插入、删除、排序、查找等操作;
(3)对于结构体类型的vector、map、set、hash_map、hash_set等,需要对运算符 ‘ < ’ 进行重载。
例如在map中引入结构体,对 ‘ < ’ 运算符进行重载:
#include<iostream>
#include<string>
#include<map>
using namespace std; struct Student
{
int num;
string name;
Student(int nu,string na) //constructor
{
name = na;
num = nu;
}
public:
bool operator< (const Student& stu) const //operator the <
{
return stu.num<num;
}
}; int main()
{
map<Student,double> mapStudent;
//student information
Student stu1(,"Christal");
Student stu2(,"Carl");
Student stu3(,"Jerry");
//insert
mapStudent.insert(pair<Student,double>(stu1,9.9));
mapStudent.insert(pair<Student,double>(stu2,8.8));
mapStudent.insert(pair<Student,double>(stu3,7.7));
map<Student,double>::iterator it;
for(it=mapStudent.begin();it!=mapStudent.end();it++)
cout<<(*it).first.num<<" "<<(*it).first.name<<" "<<(*it).second<<endl; return ;
}
STL的使用和背后数据结构的更多相关文章
- 转载:STL常用容器的底层数据结构实现
转载至:https://blog.csdn.net/qq_28584889/article/details/88763090 vector :底层数据结构为数组,支持快速随机访问 list:底层数据结 ...
- c++面试题【转】
语言部分: 虚函数,多态.这个概念几乎是必问. STL的使用和背后数据结构,vector string map set 和hash_map,hash_set 实现一个栈类,类似STL中的栈.这个题目初 ...
- STL中经常使用数据结构
STL中经常使用的数据结构: [1] stack.queue默认的底层实现为deque结构. [2] deque:用map管理多个size大小的连续内存块,方便头尾插入. [3] vector: ...
- C++的标准模板库STL中实现的数据结构之顺序表vector的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解.即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第一篇,主要针对线性表中的顺序表(动 ...
- C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用
摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...
- STL学习小结
STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...
- STL学习总结
STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合.这些" ...
- STL 较详尽总结
STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合,这些" ...
- stl 迭代器(了解)
STL 主要是由 containers(容器),iterators(迭代器)和 algorithms(算法)的 templates(模板)构成的. 对应于它们所支持的操作,共有五种 iterators ...
随机推荐
- Vue2.x中的Render函数
Render函数是Vue2.x版本新增的一个函数:使用虚拟dom来渲染节点提升性能,因为它是基于JavaScript计算.通过使用createElement(h)来创建dom节点.createElem ...
- 【二】python内置类型
1.布尔类型 表示真假的类型(true和false) 数字 0.None,以及元素为空的容器类对象都可视作False,反之为 True. In [1]: bool(0) Out[1]: False I ...
- 如何编写Hexo主题
完成一个Hexo的主题其实很简单,和写静态页面差不多,只是内容部分通过Hexo的变量去获取,而且Hexo还内置了一些辅助函数帮你快速方便地完成繁琐的处理. 起步 在写代码之前要先把项目结构搭建好,一个 ...
- wpf XAML xaml 进行 数据绑定,Resource DataContext ElementName
先做个声明:这里绑定都在前台实现,至于后台怎么写,那比前台简单多了,但更常用的是xaml中绑定.我们分析下最简单的字符串绑定来弄清楚原理,其他的类推就是. 数据绑定主要是要弄清楚两个东西,一个是源So ...
- 黑马程序员:3分钟带你读懂C/C++学习路线
随着互联网及互联网+深入蓬勃的发展,经过40余年的时间洗礼,C/C++俨然已成为一门贵族语言,出色的性能使之成为高级语言中的性能王者.而在今天,它又扮演着什么样重要的角色呢?请往下看: 后端服务器,移 ...
- Java中的框架基础面试知识
spring mvc 工作机制(原理): DispatcherServlet主要用作职责调度工作,本身主要用于控制流程 Spring mvc运行原理 1.springmvc将所有的请求都提交给Disp ...
- JDK并发包
JDK5之后引进了并发包java.util.concurrent,让并发的开发更加可控,更加简单.所以有必要好好学习下,下面从同步控制.并发容器.线程池三部分来详细了解它. 1. 各种同步控制工具的使 ...
- Android界面(1) 使用TextView实现跑马灯效果
方法一:(只能实现单个TextView的跑马灯效果)在TextView添加以下控件 android:singleLine="true"只能单行,超出的文字显示为"...& ...
- 静态代码块详解(原出处:http://versioneye.iteye.com/blog/1129579)
一 般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情 况下,其他程序来调用的时候,需要使用静态方法,这种 ...
- 安装JDK,配置环境变量有感
前天无事,心血来潮给公司新配的笔记本(win10系统64位)装开发工具,然后不可避免的就装了JDK,顺理成章的需要配置环境变量,结果就出问题了. 配置完成,测试时,在dos命令窗口输入java命令执行 ...