C++標准庫之stack(各函數及其使用全)


原創作品,轉載請注明出處:http://www.cnblogs.com/shrimp-can/p/5283207.html

棧是后入先出的。成員函數有:

1.棧的聲明

std::deque<int> mydeque (3,100);          // deque with 3 elements
std::vector<int> myvector (2,200);        // vector with 2 elements
std::stack<int> first;                    // empty stack
std::stack<int> second (mydeque); // stack initialized to copy of deque std::stack<int,std::vector<int> > third; // empty stack using vector std::stack<int,std::vector<int> > fourth (myvector); std::cout << "size of first: " << first.size() << '\n'; std::cout << "size of second: " << second.size() << '\n'; std::cout << "size of third: " << third.size() << '\n'; std::cout << "size of fourth: " << fourth.size() << '\n';
結果為:0 3 0 2

2.bool empty() const

判斷棧是否為空

stack<int> c; c.empty()

3.size_type size() const

返回棧中元素數量

c.size();

4.value_type& top();

   const value_type &top() const;

返回棧頂元素

c.top();

5.void push(const value_type& val)

在棧頂插入一個元素

c.push(value);

6.void emplace(args&& args);

在棧頂增加一個元素

c.emplace(value)

7.void pop()

出棧,即刪除棧頂元素

c.pop();

8.void swap (stack& x);

交換兩個棧中的內容

c.swap(d);

9.與vector一樣,重載了運算符:==   !=   <   <=   >   >=

參考:http://www.cplusplus.com/reference/stack/stack/

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM