当前位置:网站首页>STL标准模板库(Standard Template Library)一周学习总结
STL标准模板库(Standard Template Library)一周学习总结
2022-07-07 21:50:00 【zjsru_Beginner】
在对STL的学习过程中,我对于容器有了一定的基础认识,容器分为了8个部分+2个操作。
- list(列表)
- stack(栈-元素先进后出)
- queue(队列-元素先进先出)
- priority_queue(优先队列,)
- deque(双端队列)
- vector(向量)
- set/multiset/bitset(集合)
- map/multimap
- sort(排序操作)
- reverse /random_shuffle/unique(倒置/生成随机数/删除相邻数取只保留一个)
简单介绍一下容器所共有的特征即通用函数:
- .begin()//起始地址
- .end()//最后一个元素的下一个位置
- .size()//容器内元素,无符号整型
- .swap(b)//交换
- ::iterator//迭代器
- .empty()//判断是否为空
- .front()//返回第一个元素
- .back()//返回最后一个元素
迭代器
功能类似于指针,用来访问容器(用来保存元素的数据结构)中的元素,我自己也有点懵懂,类似于指针,它可以将你所用的结构内的元素全部遍历一遍,不会遗漏一个,使用时只要考虑上一个,下一个加上删除操作的使用,!讲解链接可以点看看,看了基本可以明白。
迭代器基于vector的代码:
for(vector<int>::iterator it=a.begin();it!=a.end();it++) cout<<*it<<endl;//a表示所用容器名,it代表一个变量可更改
以list(列表)为模板进行详细介绍
定义:list是一个双向列表,可以在常数时间内插入和删除,不支持数组表示法和随机访问。使用list时,需要引入头文件#include<list>。
ps:在写头文件的过程中,可使用#include<bits/stdc++.h>的方法偷懒。此头文件包含一切其它头文件。
list包含的专用成员函数包括
- merge(b)//合并两个有序列表,b置为空
- remove(b)//删除b的所有结点
- splice(pos,b)//在pos位置插入b
- reverse()//倒置内容
- sort()//排序
- unique()//压缩连续相同元素为一个
- push_front(x)/push_back(x)//从链表头或尾插入
- pop_front()/pop_back()//从链表头或尾去除
- front()/back()//返回头或尾的元素
- insert(p,t)//在p前插入t
- erase(p)//擦除p
- clear()//清空链表
1:首先我们通过迭代器创建一个列表,代码如下:
#include <iostream> #include <list> #include <iterator> //#include <algorithm> using namespace std; void print(list<int> &a) { for(list<int>::iterator is=a.begin(); is != a.end(); is++) cout<<" "<<*is; } int main () { list<int>first(3,6);//列表初始化为3个6 print(first); return 0; }效果图
1:merge(b)//合并两个有序列表,b置为空:
list<int>q(2,8); first.merge(q);//此时的q被置为空,将q中的内容连接到first当中 print(first);效果图:
2: remove(b)//删除b的所有结点:
first.remove(6);//指定删除链表当中所有6的数字效果图:
以下操作暂不附带图片效果自行测试!!!
3:splice(pos,b)//在pos位置插入b,b的内容可为单个数字,也可为整个列表内容。
first.splice(first.begin,a);//在first列表的开始位置插入a列表
4:reverse()//倒置内容
reverse (first.begin(),first.end());
5:sort(begin起初, end终止, compare greater<int>(类型))
//排序
first.sort();//默认升序 sort(first,fisrt+3,bool);//bool为降序功能 bool com(int x,int y) { return x>y; }
6:unique()//压缩连续相同元素为一个
first.unique();
7:push_front(x)/push_back(x)//从链表头或尾插入
push_front(i);//i为所要插入内容 push_back(i);
8:pop_front()/pop_back()//从链表头或尾去
first.pop_front(); first.pop_back();
9:front()/back()//返回头或尾的元素
cout<<first.front()<<endl; cout<<first.back()<<endl;
10:insert(p,t)//在p前插入t
int p[5]={55,66,77,88,99}; first.insert (position:first.begin(),first:p,begin:p+3);//在first开始初,插入p数组下标从0到2的元素 print(first);
11:erase(p)//擦除p
对于列表进行erase操作属实比较麻烦,在这里使用string字符串的进行简单的介绍用法
#include<bits/stdc++.h> using namespace std; int main() { string str = "wangjianwen"; cout<<str.erase(0,1)<<endl;//第一个参数为位置,第二个为长度,运行结果为angjianwen }
12:clear()//清空链表,简简单单。
first.clear();
对于STL容器而言其功能函数大同小异,大家可自行研究研究,只有自身打过代码才能记得更牢固,本节内容到此结束。
边栏推荐
- Brush question 6
- Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框
- Brush question 5
- GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
- Network security CSRF
- JMeter interface automated test read case, execute and write back result
- Clean C disk
- ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
- Mitsubishi PLC SLmP (MC) protocol
- One question per day - pat grade B 1002 questions
猜你喜欢

Cases of agile innovation and transformation of consumer goods enterprises

Wechat forum exchange applet system graduation design completion (4) opening report

GEE(四):计算两个变量(影像)之间的相关性并绘制散点图

ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions

开发那些事儿:Go加C.free释放内存,编译报错是什么原因?

【刷题记录】3. 无重复字符的最长子串

十四、数据库的导出和导入的两种方法

肠道里的微生物和皮肤上的一样吗?

Wechat forum exchange applet system graduation design (3) background function

【微服务|SCG】gateway整合sentinel
随机推荐
解决:信息中插入avi格式的视频时,提示“unsupported video format”
There is another problem just online... Warm
Network security sqlmap and DVWA explosion
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Transparent i/o model from beginning to end
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
Unity与WebGL的相爱相杀
消费品企业敏捷创新转型案例
微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
Wechat forum exchange applet system graduation design (5) assignment
【刷题记录】3. 无重复字符的最长子串
Talk about DART's null safety feature
网格(Grid)
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
Cases of agile innovation and transformation of consumer goods enterprises
GEE(三):计算两个波段间的相关系数与相应的p值
JMeter interface automated test read case, execute and write back result
智慧社區和智慧城市之間有什麼异同
为什么市场需要低代码?
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?


