STL simple application

Basic structure of STL:

STL is a general library of C + +, which consists of iterator, algorithm, container, functor, adapter and configurator (memory configurator).

1, Stack

Header file: include < stack >
Definition: stack < data < type > stack < name;
For example: stack < int > s;
Operation:
Empty () -- returns bool type, indicating whether the stack is empty (s.empty())

Size () -- return the number of elements in the stack (s.size())

Top () -- return the top element value (s.top())


Pop () -- remove the stack top element (s.pop ();)

Push (data_type a) - push an element a into the stack (s.push (a);)
Stack is an advanced data structure

Example:

#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
int main(){
stack<int>s;
s.push(1);       //Push elements 1, 2, 3 into the stack
s.push(2);       
s.push(3);       
cout <<"Top:"<<s.top()<<endl;  //Output stack top element
cout <<"Size:"<<s.size()<<endl;  //Number of elements in the output stack
s.pop();    //Remove top stack element 3        
cout <<"Size:"<<s.size()<<endl;
if(s.empty())     //Judge whether the stack is empty
{
cout <<"Is empty"<<endl;}
else{
cout <<Is not empty"<<endl;}
return 0;
}

2, queue

Header file: include < queue >

Definition: queue < data > queue > name;
For example: queue < int > Q;

Operation:
Empty() -- returns bool type, indicating whether the queue is empty (q.empty())

Size() -- return the number of elements in the queue (q.size())

Front () -- returns the next element in the queue (q.front())

Back () -- returns the last element in the queue (q.back ())

Pop () -- remove an element from the queue (q.pop ();)

Push (data_type a) - put an element a into the queue (q.push (a))

Queue is a first in, first out data structure

Example:

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
queue<int> s;
s.push(1);
s.push(2);
s.push(3);
cout <<"Front:"<<s.front()<<endl;  //Output element 1
cout <<"Back:"<<s.back()<<endl;  //Output element 3
s.pop();    //Delete element 1
cout <<"Size:"<<s.size()<<endl; 
cout <<"Front:"<<s.front()<<endl;  //Output element 2
cout <<"Back:"<<s.back()<<endl;  //Last element in output queue 3
return 0;
}

3, vector (dynamic array)

Header file: include < vector >

Definition: vector < data < type > vector < name;
For example: vector < int > V; vector < double > V; vector < string > V;

Operation:
Empty() -- returns bool type, indicating whether vector is empty (v.empty())

Size () -- returns the number of elements in the vector (v.size())

Push back (data type A) inserts element a to the end

Pop ABCD back() removes the end most element

v[i] is similar to the element (v[0]) whose array takes the ith position

vector can solve the problem of array out of bounds, and can add an element and delete an element anywhere in the array

Insert element: v.insert(v.begin()+i,a) inserts a before the i+1 element;

Delete element: v.erase(v.begin()+2) delete the third element

v.erase(v.begin()+i,v.end()+j) delete interval [i,j-1]; interval starts from 0

Empty: v.clear()

Example:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
vector<int> a;
for (int i=0;i<5;++i){
a.push_back(5-i);
}
cout<<"Size:"<<a.size()<<endl;
a.pop_back();
a[0]=1;   //Assign a value of 1 directly to the 0 th element
cout <<"Size:"<<a.size()<<endl;
for(int i=0;i<(int)a.size();++i){   //Force number of elements to integer
cout <<a[i]<<","<<endl;
}
cout <<endl;
return 0;
}

4, Sort (sort)

Header file: include < algorithm >
sort (begin,end);
sort (begin,end,cmp);

Example:
int num[]={1,5,6,2,9};

  1. sort (num,num+5); / / sort from small to large by default
    2)bool cmp(int a,int b){ return a>b;}
    sort(num,num+5,cmp);
    //From big to small

The use of sort in vector

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main()
{
	int a[10];
	vector <int> b;
	for (int i=0;i<5;i++)
	{
		cin >>a[i];
		b.push_back(a[i]);  //Input elements to vector b
	}
	sort(a,a+5);
	sort(b.begin(),b.end());  //Begin returns the location of the first element, end returns the location of the first invalid element, begin, end is the iterator object, which is used to store the location of the element
	for (int i=0;i<5;i++)
	cout <<a[i]<<" ";
	cout <<endl;
	for (int i=0;i<5;i++)
	cout <<b[i]<<" ";
	return 0;

5, Priority queue

Header file: include < queue >
Definition: priority menu < Data menu type > priority menu name;
For example: priority_queue < int > Q; / / the default is large top heap (the maximum value is in front)

Operation:
	q.push(elem) put element elem in priority queue
	q.top() returns the next element of the priority queue
	q.pop() removes an element
	q.size() returns the number of elements in the queue
	q.empty() returns whether the priority queue is empty

Priority queue is a queue with the concept of weight. It will prioritize the maximum value or minimum value at the top. The maximum value is called a heap, and the minimum value becomes a heap

Example:

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define pow2(a) ((a)*(a))     //Macro definition, a? =a*a
#define dist2(x,y)(pow2(x)+pow2(y))  //Macro definition, distance 2 = x 2 + y 2
struct coord{
	int x,y;
	const bool operator<(const coord &b)const{   //Overload operator <, compare the current point with the substitute point b, and exchange if the sum of squares of the distance between the current points is large
		return (dist2(x,y)<dist2(b.x,b.y);
	};
int main(){
priority_queue<coord> s;   //Put the structure in the priority queue
coord a;
a.x=3,a.y=2;
s.push(a);    //Input (3, 2) = 13
a.x=1,a.y=2;
s.push(a);    //Input (1, 2) = 5
a.x=2,a.y=2;
s.push(a);    //Input (2, 2) = 8
cout <<"Size:"<<s.size()<<endl;
cout <<"Top:"<<s.top().x<<","<<s.top().y<<endl;  //Output large top reactor (3, 2)
s.pop();   //Delete large top heap (3, 2)
cout <<"Top:"<<s.top().x<<","<<s.top().y<<endl;  //Output the large top reactor at this time (2, 2)
return 0;
}
Published 9 original articles, won praise 3, visited 180
Private letter follow

Added by plex303 on Wed, 11 Mar 2020 07:52:50 +0200