Standard template library and STL algorithm
Generic: It
is the core idea of the standard template library. The so-called generic means that the object is separated from the data type to which the object belongs.
STL is a generic library:
all its components are composed of templates, and its elements can be of any type.
The C++ standard library consists of 6 major components:
(1) Generic algorithm;
(2) Generic container;
(3) Generic iterator;
(4) Function object;
(5) Adapter;
(6) Allocator;
1. Container: Manage a collection of objects of a certain type;
2. Iterator: traverse the collection of objects;
3. Algorithm: Provide algorithms for processing elements in the collection; C++ standard, STL is organized in 13 header files,
One, STL algorithm
STL uses function templates to define more than 70 commonly used algorithms. The algorithms are mainly included in,,
commonly used:
1. sort
code:
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a[]={
2,5,8,6,8,5,55,88,8};
int len=(sizeof(a)/sizeof(*a));
sort(a,a+len);
for(int i=0;i<len;i++){
cout<<a[i]<<' ';
}
return 0;
}
Extension (sort is very useful, sort (a, a+n) is the default sort from small to large, you can also sort from large to small according to your needs, and you can even sort the structure);
code:
//使用sort实现结构体排序
#include<iostream>
#include<algorithm>
using namespace std;
struct Point{
int x;
int y;
}p[4];
bool cmp(Point p1,Point p2){
if(p1.x==p2.x){
return p1.y<p2.y;
}else{
return p1.x<p2.x;
}
}
int main(){
for(int i=0;i<4;i++){
cin>>p[i].x;
cin>>p[i].y;
}
sort(p,p+4,cmp);
for(int i=0;i<4;i++){
cout<<"("<<p[i].x<<","<<p[i].y<<")"<<endl;
}
return 0;
}
Second, count (returns the number of times the key appears from the start point to the end point)
code:
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
string str("one world one dream");
char key='e';
n=count(str.begin(),str.end(),key);
cout<<"key count:"<<n<<endl;
return 0;
}
Three, other algorithms
Second, the container
STL containers are used to store and hold various types of data. They are implemented as class templates.
STL containers are divided into three categories:
1) sequential containers;
2) associative containers;
3) container adapters;
1. sequential containers;
one, vector (vector): elements stored continuously;
two, list (list): by nodes The two-way linked list is composed, each node contains one;
three, deque: an array of pointers to different elements stored continuously;
2. associative container:
one, set: can not have the same elements;
Two, multiple set (meltiset): can have the same elements;
three, map (map): a set composed of {key, value};
3. Container adapter:
one, stack (stack): first in last out
two, queue ( queue): first in first out
3. Priority queue (priority_queue): the higher priority is in the front;
Note:
Examples of vector usage:
code:
#include<vector>
#include<iostream>
using namespace std;
int main(){
vector <int> vi;
int a;
while(true){
cout<<"输入一个整数,按0停止"<<endl;
cin>>a;
if(a==0)break;
vi.push_back(a);
}
for(int i=0;i<vi.size();i++){
cout<<vi[i]<<" ";
}
return 0;
}
Create a vector to
access the elements in the
vector insert or delete the reverse order and copy of the vector function
:
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int arr[6]={
1,25,25,88,11,2};
vector<int> v1(arr,arr+6),v2;//v1的初始化
v2.resize(6);//发配大小
copy(v1.begin(),v1.end(),v2.begin());//复制函数
reverse(v2.begin(),v2.end());//逆序函数
for(int i=0;i<v2.size();i++){
cout<<v2[i]<<" ";
}
return 0;
}
Three, iterator
Iterator is a encapsulated high-level pointer. Users can traverse the container or sequence without caring about the data type of the object; the
iterator is in the header file :,, the
iterator traversal process :
begin() and end() : the
iterator traverses the instance:
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int arr[6]={
1,25,25,88,11,2};
vector<int> v1(arr,arr+6);
for(vector<int>::iterator it=v1.begin();it!=v1.end();it++){
cout<<*it<<' ';
}
return 0;
}
Advanced exercise : (Orderly merging of two linked lists)
input_case:
3 3
-1 22 8
9 -88 2
output_case:
-88 -1 2 8 9 22
code:
#include<iostream>
#include<list>
using namespace std;
int main(){
list<int> l1,l2;
int len_1,len_2;//两个链表的长度
scanf("%d %d",&len_1,&len_2);
int num;
for(int i=0;i<len_1;i++){
cin>>num;
l1.push_back(num);//加入链表
}
for(int i=0;i<len_2;i++){
cin>>num;
l2.push_back(num);//加入链表
}
l1.sort();//表1排序
l2.sort();//表2排序
l1.merge(l2);//l2归并到l1中
for(list<int>::iterator it=l1.begin();it!=l1.end();it++){
//迭代器遍历
cout<<*it<<' ';
}
return 0;
}
Iterator type:
Whether different containers support iterators:
Note : Iterators can also be assigned via subscripts (only applicable to those that support random access), such as vector, but not list