当前位置:网站首页>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容器而言其功能函数大同小异,大家可自行研究研究,只有自身打过代码才能记得更牢固,本节内容到此结束。
边栏推荐
猜你喜欢

JMeter-接口自动化测试读取用例,执行并结果回写

微信论坛交流小程序系统毕业设计毕设(5)任务书

Understand the session, cookie and token at one time, and the interview questions are all finalized

微生物健康网,如何恢复微生物群落

Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!

微信论坛交流小程序系统毕业设计毕设(7)中期检查报告

数据库每日一题---第22天:最后一次登录

知识点滴 - PCB制造工艺流程

Unity与WebGL的相爱相杀

Software test classification
随机推荐
安踏DTC | 安踏转型,构建不只有FILA的增长飞轮
解决:信息中插入avi格式的视频时,提示“unsupported video format”
Handling file exceptions
Exploratory data analysis of heartbeat signal
Byte hexadecimal binary understanding
Wechat forum exchange applet system graduation design (3) background function
About idea cannot find or load the main class
CTF exercise
What does the model number of asemi rectifier bridge kbpc1510 represent
聊聊 Dart 的空安全 (null safety) 特性
V20变频器手自动切换(就地远程切换)的具体方法示例
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
leetcode-520. 检测大写字母-js
Why does the market need low code?
网络安全-钓鱼
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
Txt file virus
十四、数据库的导出和导入的两种方法
2021-01-11
Personal statement of testers from Shuangfei large factory: is education important for testers?


