Notes (stack queue calls in the STL library)

//sort
#include<bits/stdc++.h>
using namespace std;
bool compare(int a,int b)
{
    return a<b;//From small to large
}

intmain()
{
    int a[5];
    for(int i=0;i<5;i++)
    cin>>a[i];
    sort(a+0,a+5,compare);//sort sort only sort a[0] to a[4] excluding a[5]
    for(int i=0;i<5;i++)cout<<a[i]<<" ";
}

compare a,b, size

//min max
#include<bits/stdc++.h>
using namespace std;
intmain()
{
	int a=1,b=2;
	cout<<min(a,b)<<endl;
	cout<<max(a,b);
}

Fast exponentiation modulo

//quick exponentiation modulo
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll pow_mod(ll x,ll n,ll mod)
{
	ll res=1;//Save the result
	while(n)
	{
		if(n&1==1)
		res=res*x%mod;
		
		x=x*x%mod;
		n=n/2;
	}
	return res;
}
intmain()
{
	ll x,n,mod;
	cin>>x>>n>>mod;
	ll res;
	res=pow_mod(x,n,mod);
	cout<<res;
}

kmp algorithm template

// KMP
//Test data ababaaababaa  
#include<bits/stdc++.h>  
using namespace std;  
int kmp(char *t,char *p,int *next);  
void getnext(char *p,int *next);  
intmain()  
{  
    char a[100],b[100];  
    gets(a);//Known string   
    gets(b);//The given string   
    int l=strlen(a);  
    int next[l+1];  
    getnext(a,next);//Get the next array value of the known string    
    int flag=kmp(a,b,next);//Use kmp to return the position of the matching string in the main string   
    cout<<flag;   
}   
void getnext(char p[],int next[])  
{  
    next[0]=-1;  
    int i=0,j=-1;  
    while(i<strlen(p))  
    {  
        if(j==-1||p[i]==p[j])  
        {  
            ++i;  
            ++j;  
            next[i]=j;  
        }  
        else  
        {  
            j=next[j];  
        }  
    }  
}  
int kmp(char *t,char *p,int *next)  
{  
    int i=0,j=0;  
    while(i<strlen(t)&&j<strlen(p))  
    {  
        if(j==-1||t[i]==p[j])  
        {  
            i++;  
            j++;  
        }  
        else  
        {  
            j=next[j];  
        }  
    }  
    if(j==strlen(p))  
    {  
        return i-j;  
    }  
    else  
    {  
        return -1;  
    }  
}  

stack queue priority queue call

https://www.bilibili.com/video/av7970151?from=search&seid=17110722423615707865

stack call

#include<bits/stdc++.h>
#include<stack>
using namespace std;
intmain()
{
	stack<int>p;
	int x;
	for(int i=0;i<5;i++)
	{
		cin>>x;
		p.push(x);
	}
	cout<<p.size()<<"\n";
	while(!p.empty())
	{
		cout<<p.top()<<" ";
		p.pop();
		
	}
}

Queue call:

#include<bits/stdc++.h>
#include<queue>
using namespace std;
intmain()
{
	queue<int> q;
	int x;
	for(int i=0;i<5;i++)
	{
		cin>>x;
		q.push(x);
	}
	cout<<q.size()<<"\n";
	while(!q.empty())
	{
		cout<<q.front()<<" ";
		q.pop();
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472577&siteId=291194637