当前位置:网站首页>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容器而言其功能函数大同小异,大家可自行研究研究,只有自身打过代码才能记得更牢固,本节内容到此结束。
边栏推荐
- Sword finger offer 27 Image of binary tree
- Gbu1510-asemi power supply special 15A rectifier bridge gbu1510
- Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
- iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
- Microbial Health Network, How to restore Microbial Communities
- Cases of agile innovation and transformation of consumer goods enterprises
- opencv scalar传入三个参数只能显示黑白灰问题解决
- 三菱PLC slmp(mc)协议
- The author of LinkedList said he didn't use LinkedList himself
- Understand the session, cookie and token at one time, and the interview questions are all finalized
猜你喜欢

Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors

Specific method example of V20 frequency converter manual automatic switching (local remote switching)

Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?

Gee (IV): calculate the correlation between two variables (images) and draw a scatter diagram
![Cause analysis and solution of too laggy page of [test interview questions]](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Cause analysis and solution of too laggy page of [test interview questions]

The author of LinkedList said he didn't use LinkedList himself

微信论坛交流小程序系统毕业设计毕设(7)中期检查报告
![[record of question brushing] 3 Longest substring without duplicate characters](/img/44/1cd8128d93c9c273e0f4718d84936e.png)
[record of question brushing] 3 Longest substring without duplicate characters

消息队列与快递柜之间妙不可言的关系

GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
随机推荐
The author of LinkedList said he didn't use LinkedList himself
Mitsubishi PLC SLmP (MC) protocol
解决:信息中插入avi格式的视频时,提示“unsupported video format”
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
Byte hexadecimal binary understanding
Brush question 6
Bit operation
Brush question 4
网络安全-burpsuit
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
PMP project management exam pass Formula-1
About idea cannot find or load the main class
Txt file virus
Wechat forum exchange applet system graduation design (5) assignment
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
双非大厂测试员亲述:对测试员来说,学历重要吗?
Talk about DART's null safety feature
Grid
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
Clean C disk


