C++ STL stack使用

C++ stack


类模板:

template<
    class T,
    class Container = std::deque<T>
> class stack;
  • 定义于头文件 <stack>
  • std::stack 类是容器适配器,它给予程序员栈的功能——特别是 FILO (先进后出)数据结构。
  • 该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶的容器尾部推弹元素。

创建stack

构造函数

定义:

// 默认构造函数
stack() : stack(Container()) { }

// 以 cont 的内容复制构造底层容器 c 
explicit stack( const Container& cont );

// 以 std::move(cont) 移动构造底层容器 c 
explicit stack( Container&& cont );

// 复制构造函数
stack( const stack& other );

// 复制构造函数
stack( stack&& other );

用法:

    std::stack<int> c1;
 
    std::stack<int> c2(c1);
 
    std::deque<int> deq {3, 1, 4, 1, 5};
    std::stack<int> c3(deq);

operator=

  • 赋值给容器适配器

定义:

stack& operator=( const stack& other );
stack& operator=( stack&& other );

用法:

stack<int> s1;
stack<int> s2;

s1 = s2;
s1 = std::move(s2);

元素访问

top

  • 访问栈顶元素

定义:

reference top();
const_reference top() const;

用法:

 	std::stack<int> s;
    s.push( 2 );
    s.push( 6 );
    s.push( 51 );

	std::cout << s.top() << std::endl;		// 51

容量

empty

  • 检查底层的容器是否为空
bool empty() const;
  • 返回值
    若底层容器为空则为 true ,否则为 false 。

size

  • 返回容纳的元素数
size_type size() const;

修改器

push

  • 向栈顶插入元素
// 等效地调用 c.push_back(value)
void push( const value_type& value );

// 等效地调用 c.push_back(std::move(value))
void push( value_type&& value );

emplace

  • 于顶原位构造元素
template< class... Args >
void emplace( Args&&... args );

pop

  • 删除栈顶元素
  • 无返回值
void pop();

swap

  • 交换内容
void swap( stack& other ) noexcept(/* see below */);

非成员函数

operator==、!=、<、<=、>、>=、<=>

按照字典顺序比较 stack 中的值

template< class T, class Container >
bool operator==( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, class Container >
bool operator!=( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, class Container >
bool operator<( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, class Container >
bool operator<=( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, class Container >
bool operator>( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, class Container >
bool operator>=( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

template< class T, std::three_way_comparable Container >
std::compare_three_way_result_t<Container>
    operator<=>( const std::stack<T,Container>& lhs, const std::stack<T,Container>& rhs );

std::swap(std::stack)

  • 特化 std::swap 算法
template< class T, class Container >
void swap( std::stack<T,Container>& lhs,
           std::stack<T,Container>& rhs );
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值