当前位置:网站首页>One week learning summary of STL Standard Template Library
One week learning summary of STL Standard Template Library
2022-07-07 23:25:00 【zjsru_ Beginner】
In the face of STL In the process of learning , I have a certain basic understanding of containers , Containers are divided into 8 Parts of +2 Operations .
- list( list )
- stack( Stack - Elements come in and out )
- queue( queue - Element first in first out )
- priority_queue( Priority queue ,)
- deque( deque )
- vector( vector )
- set/multiset/bitset( aggregate )
- map/multimap
- sort( The sorting operation )
- reverse /random_shuffle/unique( The horse / Generate random number / Delete adjacent data fetches and keep only one )
Briefly introduce the common features of containers, namely, general functions :
- .begin()// Initial address
- .end()// The next position of the last element
- .size()// Elements in the container , Unsigned integer
- .swap(b)// In exchange for
- ::iterator// iterator
- .empty()// Determine whether it is null
- .front()// Returns the first element
- .back()// Return to the last element
iterator
Function similar to pointer , Used to access containers ( The data structure used to hold elements ) The elements in , I'm a little confused myself , It's like a pointer , It can traverse all the elements in the structure you use , You won't miss one , When using, just consider the previous , Next, add the use of delete operation ,! You can click the explanation link , After reading it, I can basically understand .
Iterator based vector Code for :
for(vector<int>::iterator it=a.begin();it!=a.end();it++) cout<<*it<<endl;//a Indicates the container name used ,it Represents a variable that can be changed
With list( list ) Introduce the template in detail
Definition :list It's a two-way list , You can insert and delete... In constant time , I won't support it Array representation and random access . Use list when , Need to introduce header file #include<list>.
ps: In the process of writing header file , You can use #include<bits/stdc++.h> The way to be lazy . This header file contains all other header files .
list The included special member functions include
- merge(b)// Merge two sequential tables ,b Set to empty
- remove(b)// Delete b All of the nodes
- splice(pos,b)// stay pos Position insert b
- reverse()// Invert the content
- sort()// Sort
- unique()// Compress the same elements consecutively into one
- push_front(x)/push_back(x)// Insert from the head or tail of the chain
- pop_front()/pop_back()// Remove from the head or tail of the chain
- front()/back()// Return the element of the head or tail
- insert(p,t)// stay p Insert before t
- erase(p)// erase p
- clear()// Empty the list
1: First, we create a list through the iterator , The code is as follows :
#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);// The list is initialized to 3 individual 6 print(first); return 0; }
design sketch
1:merge(b)// Merge two sequential tables ,b Set to empty :
list<int>q(2,8); first.merge(q);// At this time q Set to blank , take q Content in connects to first among print(first);
design sketch :
2: remove(b)// Delete b All of the nodes :
first.remove(6);// Specify to delete all in the linked list 6 The number of
design sketch :
The following operations are not attached to the picture effect for self-test !!!
3:splice(pos,b)// stay pos Position insert b,b The content of can be a single number , It can also be the content of the whole list .
first.splice(first.begin,a);// stay first Insert at the beginning of the list a list
4:reverse()// Invert the content
reverse (first.begin(),first.end());
5:sort(begin At first , end End , compare greater<int>( type ))
// Sort
first.sort();// Default ascending order sort(first,fisrt+3,bool);//bool It is a descending function bool com(int x,int y) { return x>y; }
6:unique()// Compress the same elements consecutively into one
first.unique();
7:push_front(x)/push_back(x)// Insert from the head or tail of the chain
push_front(i);//i Insert content for push_back(i);
8:pop_front()/pop_back()// Go from the head or tail of the chain
first.pop_front(); first.pop_back();
9:front()/back()// Return the element of the head or tail
cout<<first.front()<<endl; cout<<first.back()<<endl;
10:insert(p,t)// stay p Insert before t
int p[5]={55,66,77,88,99}; first.insert (position:first.begin(),first:p,begin:p+3);// stay first Beginning , Insert p Array index from 0 To 2 The elements of print(first);
11:erase(p)// erase p
For the list erase The operation is really troublesome , Use it here string A simple introduction to the use of strings
#include<bits/stdc++.h> using namespace std; int main() { string str = "wangjianwen"; cout<<str.erase(0,1)<<endl;// The first parameter is position , The second is length , The running result is angjianwen }
12:clear()// Empty the list , A simple .
first.clear();
about STL For containers, their function functions are similar , You can study by yourself , Only after you have typed the code can you remember it more firmly , This is the end of this section .
边栏推荐
- Wechat forum exchange applet system graduation design completion (7) Interim inspection report
- Opencv scalar passes in three parameters, which can only be displayed in black, white and gray. Solve the problem
- 海内外技术人们“看”音视频技术的未来
- 给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
- Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
- 七月第一周
- The text editor of markdown class should add colors to fonts (including typora, CSDN, etc.)
- 云原生数据仓库AnalyticDB MySQL版用户手册
- Why does the market need low code?
- 14、 Two methods of database export and import
猜你喜欢
伸展树(一) - 图文解析与C语言实现
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
ArcGIS: two methods of attribute fusion of the same field of vector elements
[microservices SCG] gateway integration Sentinel
Wechat forum exchange applet system graduation design (2) applet function
USB(十五)2022-04-14
LDO稳压芯片-内部框图及选型参数
Binary tree
Wechat forum exchange applet system graduation design (3) background function
LDO voltage stabilizing chip - internal block diagram and selection parameters
随机推荐
Cloud native is devouring everything. How should developers deal with it?
v-for遍历对象
USB(十五)2022-04-14
LDO穩壓芯片-內部框圖及選型參數
ROS2专题(03):ROS1和ROS2的区别【01】
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
System design overview
七月第一周
POJ2392 SpaceElevator [DP]
Gee (III): calculate the correlation coefficient between two bands and the corresponding p value
LM12丨Rolling Heikin Ashi二重K线滤波器
ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
V-for traversal object
Archlinux install MySQL
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
Wechat forum exchange applet system graduation design completion (7) Interim inspection report
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
Description of longitude and latitude PLT file format
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化