C ++テンプレートライブラリのSTL - スタック

良いホイール〜作られた
0、アクセス(スタックのアクセスのみ上部)

#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に示すように、関数のいくつかの
ポップ()

#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;
}

空の()

#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;
}

サイズ()
スタック内の要素の数を返します

#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;
}
公開された121元の記事 ウォンの賞賛3 ビュー2966

おすすめ

転載: blog.csdn.net/weixin_42377217/article/details/104093278