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/

 

 

 

 

转载于:https://www.cnblogs.com/shrimp-can/p/5283207.html

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值