清空_stack_temp_three_means

//

01 st.pop() 逐个清空

    while( st.size() )      st.pop();
    while( !st.empty() )    st.pop();

02 赋空

    st=stack<T>();

03 置换

    swap( st,st1 );         // 需要额外一个空的栈
    stack<T>().swap( st );  // 需要高版本c++

// eg.
#include<bits/stdc++.h>
using namespace std;

stack<int> st,st1;

int main()
{
    int n,i;

    while( cin>>n )
    {
        for( i=0;i<n;i++ ) st.push(i);
        
//        while( st.size() ) st.pop();

//        while( !st.empty() ) st.pop();
		
		swap( st,st1 );
		
//        st=stack<int>();

//        stack<int>().swap( st );
		
        cout<<st.size()<<endl;
    }
    return 0;
}

おすすめ

転載: blog.csdn.net/qq_63173957/article/details/123633013