[C++]stack源码解析

本文深入解析C++标准库中stack的实现,重点讲解其默认使用deque作为底层容器以及如何利用move语义。文章首先补充了关于enabled_if和条件编译的知识,然后展示了stack的源码,并提及了deque接口的相关内容。
摘要由CSDN通过智能技术生成

stack源码解析

stack是默认使用deque(双头队列)来实现的。实际上如果使用结构体指针也是可以做到相同功能的。以下提供stack源码。重点学习其头文件的写法。

首先补充一点知识:

enabled_if:

template <bool Cond, class T = void> struct enable_if;
Enable type if condition is met
The type T is enabled as member type enable_if::type if Cond is true.

Otherwise, enable_if::type is not defined.

This is useful to hide signatures on compile time when a particular condition is not met, since in this case, the memberenable_if::type will not be defined and attempting to compile using it should fail.

It is defined with a behavior equivalent to:
1 template<bool Cond, class T = void> struct enable_if {};
2 template<class T> struct enable_if<true, T> { typedef T type; };

这是来自cplusplus的解释。就是说,Cond(即condition)一定要是true,才能通过编译。看例子,来具体分析:

而关于move的转移语义。具体看前文。右值引用和转移语义

// enable_if example: two ways of using enable_if
#include <iostream>
#include <type_traits>
// 1. the return type (bool) is only valid if T is an integral type:
template <class T>
typename std::enable_if<std::is_integral<T>::value,bool>::type
is_odd (T i) {
  return bool(i%2);}

// 2. the second template argument is only valid if T is an integral type:
template < class T,
class = typename std::enable_if<std::is_integral<T>::value>::type>
bool is_even (T i) {
  return !bool(i%2);}

int main() {

    int i = 1;    // code does not compile if type of i is not integral

    std::cout << std::boolalpha;
    std::cout << "i is odd: " << is_odd(i) << std::endl;
    std::cout << "i is even: " << is_even(i) << std::endl;

    return 0;
}

而关于move的转移语义。具体看前文。右值引用和转移语义

源码

// -*- C++ -*-
//===---------------------------- stack -----------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP_STACK
#define _LIBCPP_STACK

/*
    stack synopsis

namespace std
{

template <class T, class Container = deque<T>>
class stack
{
public:
    typedef Container                                container_type;
    typedef typename container_type::value_type      value_type;
    typedef typename container_type::reference       reference;
    typedef typename container_type::const_reference const_reference;
    typedef typename container_type::size_type       size_type;

protected:
    container_type c;

public:
    stack() = default;
    ~stack() = default;

    stack(const stack& q) = default;
    stack(stack&& q) = default;

    stack& operator=(const stack& q) = default;
    stack& operator=(stack&& q) = default;

    explicit stack(const container_type& c);
    explicit stack(container_type&& c);
    template <class Alloc> explicit stack(const Alloc& a);
    template <class Alloc> stack(const container_type& c, const Alloc& a);
    template <class Alloc> stack(container_type&& c, const Alloc& a);
    template <class Alloc> stack(const stack& c, const Alloc& a);
    template <class Alloc> stack(stack&& c, const Alloc& a);

    bool empty() const;
    size_type size() const;
    reference top();
    const_reference top() const;

    void push(const value_type& x);
    void push(value_type&& x);
    template <class... Args> void emplace(Args&&... args);
    void pop();

    void swap(stack& c) noexcept(noexcept(swap(c, q.c)));
};

template <class T, class Container>
  bool operator==(const stack<T, Container>& x, const stack<T, Container>& y);
template <class T, class Container>
  bool operator< (const stack<T, Container>& x, const stack<T, Container>& y);
template <class T, class Container>
  bool operator!=(const stack<T, 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要清空一个C++的stack容器,有多种方法可以实现。 第一种方法是通过拷贝构造函数来复制一个空的stack容器。你可以使用以下代码实现: ```cpp std::stack<int> copy_stack = my_stack; ``` 这将创建一个新的stack容器copy_stack,它是一个空的副本。 第二种方法是通过直接创建一个新的空stack容器并将其与原来的stack容器进行交换。这可以通过以下代码实现: ```cpp std::stack<elemType>().swap(s); ``` 这将创建一个临时的空stack容器,然后使用swap函数将其与原来的stack容器s进行交换。最后,临时stack容器会被销毁,同时释放所有的内存空间。这种方法比逐个弹出元素要快。 第三种方法是通过循环将stack容器中的元素一个一个弹出来清空。你可以使用以下代码实现: ```cpp while (!s.empty()) { s.pop(); } ``` 这个方法逐个检查stack容器是否为空,如果不为空则调用pop函数弹出栈顶元素,直到stack容器为空为止。虽然这种方法比较繁琐,但也是一种有效的清空stack容器的方法。 所以,以上是三种清空C++的stack容器的方法。你可以根据自己的需求选择合适的方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [[C++]stack使用总结](https://blog.csdn.net/qq525003138/article/details/107052849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值