C ++ template library STL - stack

Has made a good wheel ~
0, access (access only top of the stack)

#include <stdio.h>
#include <stack>
using namespace std;

int main(){
	stack<int>st;
	for(int i = 1;i <= 5;i++){
		st.push(i);
	}
	printf("%d\n",st.top());//访问栈顶元素(也只能这么访问) 
	return 0;
}

1, some of the function
pop ()

#include <stdio.h>
#include <stack>
using namespace std;

int main(){
	stack<int>st;
	for(int i = 1;i <= 5;i++){
		st.push(i);
	}
	for(int i = 1;i <= 3;i++){
		st.pop();//连续三次将栈顶元素出栈 
	}
	printf("%d\n",st.top());
	return 0;
}

empty()

#include <stdio.h>
#include <stack>
using namespace std;

int main(){
	stack<int>st;
	if(st.empty() == true){
		printf("Empty\n");
	}
	else{
		printf("Not Empty\n");
	}
	st.push(1);
	if(st.empty() == true){
		printf("Empty\n");
	}
	else{
		printf("Not Empty\n");
	}
	return 0;
}

size ()
returns the number of elements in the stack

#include <stdio.h>
#include <stack>
using namespace std;

int main(){
	stack<int>st;
	for(int i = 1;i <= 5;i++){
		st.push(i);
	}
	printf("%d\n",st.size());
	return 0;
}
Published 121 original articles · won praise 3 · Views 2966

Guess you like

Origin blog.csdn.net/weixin_42377217/article/details/104093278