当前位置:网站首页>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容器而言其功能函数大同小异,大家可自行研究研究,只有自身打过代码才能记得更牢固,本节内容到此结束。
边栏推荐
- Are the microorganisms in the intestines the same as those on the skin?
- 为什么市场需要低代码?
- 网络安全-联合查询注入
- What does the model number of asemi rectifier bridge kbpc1510 represent
- 2022 words for yourself
- ./ setup. Insufficient sh permission
- Talk about DART's null safety feature
- Microbial Health Network, How to restore Microbial Communities
- Cause analysis and solution of too laggy page of [test interview questions]
- 网络安全-burpsuit
猜你喜欢
Wechat forum exchange applet system graduation design completion (6) opening defense ppt
微信论坛交流小程序系统毕业设计毕设(4)开题报告
Understand the session, cookie and token at one time, and the interview questions are all finalized
知识点滴 - PCB制造工艺流程
Brush question 4
Wechat forum exchange applet system graduation design (3) background function
Knowledge drop - PCB manufacturing process flow
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
ArcGIS:字段赋值_属性表字段计算器(Field Calculator)依据条件为字段赋值
PMP项目管理考试过关口诀-1
随机推荐
Network security - install CentOS
Cases of agile innovation and transformation of consumer goods enterprises
The wonderful relationship between message queue and express cabinet
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
Two kinds of curves in embedded audio development
Software evaluation center ▏ what are the basic processes and precautions for automated testing?
2021-01-11
Network security - phishing
Why is network i/o blocked?
数据库每日一题---第22天:最后一次登录
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
【测试面试题】页面很卡的原因分析及解决方案
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
What are the similarities and differences between smart communities and smart cities
opencv scalar传入三个参数只能显示黑白灰问题解决
网络安全-beef
JMeter-接口自动化测试读取用例,执行并结果回写
Wechat forum exchange applet system graduation design completion (8) graduation design thesis template