C++学习之路(3) 常用数据结构与STL
来源:互联网 发布:淘宝手机比京东便宜 编辑:程序博客网 时间:2024/05/21 23:33
一.什么是STL
STL全称为Standard Template Library,即标准模板库,该库提供一些常用的容器对象和一些通用的算法等,大家可以理解STL就是一个库,该库帮我们封装了很多容器类和通用的方法,我们可以通过调用该库中封装好的方法和容器类来进行编程
二.STL 六大组件
- 容器(Container)
- 算法(Algorithm)
- 迭代器(Iterator)
- 函数对象,又称仿函数(Function object)
- 适配器(Adaptor)
- 空间配置器(Allocator)
本文主要讨论容器组件。
容器
STL中容器可分为序列式容器和关联式容器,其中,序列式容器的每个元素的位置取决于元素被插入时设置的位置,和元素值本身无关,而,关联式容器的元素位置取决于特定的排序规则,和插入顺序无关。意思就说,序列式容器中每个元素的位置与插入的顺序对应,而关联式容器中元素会根据特定的排序规则对每个元素进行排序,与元素插入的顺序无关。
序列式容器包括:vector、list和deque,而关联式容器有:set、multiset、map和multimap
vector适合模拟栈,deque适合模拟队列,list适合模拟链表。
算法
算法是用来操作容器中数据的模板函数,它抽象了对数据结构的操作行为。要使用STL中定义的算法,应首先引入头文件。例如STL中的sort()函数可以对容器中的数据进行排序,可以使用find()函数来搜索容器中的某个元素。
迭代器
STL实现要点是将容器和算法分开,使两者彼此独立。迭代器使两个联系起来,迭代器提供访问容器中的方法。迭代器实质上是一种智能指针,它重载了->和*操作符。事实上,C++指针也是一种迭代器。
四.STL demo
vector
#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(void){ int a[]={1,3,4,6,8,7}; vector<int> arr(a,a+6); vector<int> ::iterator begin; /*Initialize a vector*/ cout<<"Initialize vector arr with a:"<<endl; for(begin=arr.begin();begin!=arr.end();begin++) cout<<*begin<<" "; cout<<endl; /*Sort the vector*/ cout<<"After arr was sorted:"<<endl; sort(arr.begin(),arr.end()); for(begin=arr.begin();begin!=arr.end();begin++) cout<<*begin<<" "; cout<<endl; /*pop*/ arr.pop_back(); cout<<"After pop_back:"<<endl; for(begin=arr.begin();begin!=arr.end();begin++) cout<<*begin<<" "; cout<<endl; /*push*/ arr.push_back(8); cout<<"After push_back:"<<endl; for(begin=arr.begin();begin!=arr.end();begin++) cout<<*begin<<" "; cout<<endl; /*search*/ const int FINDNUMBER=4; cout<<"Find the element which value is 4:"<<endl; vector<int> ::iterator search=find(arr.begin(),arr.end(),FINDNUMBER); if(search!=arr.end()) cout<<"Find"<<" "<<*search<<" "<<"SUCCESS"<<endl; else cout<<"FAILED"<<endl; /*clear*/ cout<<"Destroy the vector"<<endl; arr.clear();}
deque
#include<iostream>#include<deque>#include<algorithm>using namespace std;int main(void){ int arr[]={5,8,9,4,6,2}; /*Initialize the queue*/ cout<<"Initialize the queue"<<endl; deque<int> que(arr,arr+6); deque<int> ::iterator pos; /*Traverse the queue*/ cout<<"Traverse the queue"<<endl; for(pos=que.begin();pos!=que.end();pos++) cout<<*pos<<" "; cout<<endl; /*push_back*/ cout<<"push_back"<<endl; que.push_back(10); for(pos=que.begin();pos!=que.end();pos++) cout<<*pos<<" "; cout<<endl; /*push_front*/ cout<<"push_front 5"<<endl; que.push_front(5); for(pos=que.begin();pos!=que.end();pos++) cout<<*pos<<" "; cout<<endl; /*pop_front*/ cout<<"pop_front"<<endl; que.pop_front(); for(pos=que.begin();pos!=que.end();pos++) cout<<*pos<<" "; cout<<endl; /*pop_back*/ cout<<"pop_back"<<endl; que.pop_back(); for(pos=que.begin();pos!=que.end();pos++) cout<<*pos<<" "; cout<<endl; /*search number 9*/ cout<<"search number 9"<<endl; deque<int>:: iterator search=find(que.begin(),que.end(),9); if(search!=que.end()) cout<<"Find"<<" "<<*search<<" "<<"SUCCESS"<<endl; else cout<<"FAILED";}
list
#include<iostream>#include<list>#include<algorithm>using namespace std;int main(void){ /*Initialize Linklist*/ int arr[]={3,8,9,4,6,2,4,8}; list<int> Linklist(arr,arr+8); list<int> ::iterator pmove; /*isEmpty?*/ if(Linklist.empty()) cout<<"Linklist is empty"<<endl; else cout<<"Linklist is not empty"<<endl; /*Traverse*/ cout<<"Traverse Linklist"<<endl; for(pmove=Linklist.begin();pmove!=Linklist.end();pmove++) cout<<*pmove<<" "; cout<<endl; /*push_back*/ cout<<"Push_back 5"<<endl; Linklist.push_back(5); for(pmove=Linklist.begin();pmove!=Linklist.end();pmove++) cout<<*pmove<<" "; cout<<endl; /*find push_front pop_front and pop_back is same with oush_back operation, I dont wanna repeat*/ /*count*/ cout<<"Count"<<endl; int Count=count(Linklist.begin(),Linklist.end(),4); cout<<Count;}
0 0
- C++学习之路(3) 常用数据结构与STL
- 2.Python调用C语言之常用变量与数据结构
- 数据结构与C++stl(2) 仿函数
- 1806:词典( 3.9数据结构之C++STL)
- C++STL之map学习
- C++STL之map学习
- 数据结构与算法分析之c++STL的vector特点分析
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础zz
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 学习STL map, STL set之数据结构基础
- 剑指offer第12题 最优解
- Android学习笔记035之HTTP协议
- webView的使用及注意事项
- webapp开发使用Cordova-Cordova教程
- poj 2349(Prime + Kruskal 最小生成树)
- C++学习之路(3) 常用数据结构与STL
- 使用SpanableString实现textview部分字体点击事件(不同颜色)
- Eclipse开发C/C++环境配置
- 01背包
- JS构造Map结构
- 完数-week6-C语言习题集
- revit二次开发基础
- Linux命令--grep
- Linux初学者必备命令