简体   繁体   English

如何获得“基于堆栈”的 std::stack? (具有固定的最大尺寸)

[英]How to get a 'stack based' std::stack? (with fixed max size)

According to std::stack reference:根据std::stack参考:

The type of the underlying container to use to store the elements.用于存储元素的底层容器的类型。 The container must satisfy the requirements of SequenceContainer.容器必须满足 SequenceContainer 的要求。 Additionally, it must provide the following functions with the usual semantics:此外,它必须提供以下具有通常语义的函数:

 back() push_back() pop_back()

The standard containers std::vector, std::deque and std::list satisfy these requirements.标准容器 std::vector、std::deque 和 std::list 满足这些要求。 By default, if no container class is specified for a particular stack class instantiation, the standard container std::deque is used.默认情况下,如果没有为特定堆栈 class 实例化指定容器 class,则使用标准容器 std::deque。

All of which are heap based containers.所有这些都是基于堆的容器。

The following actually compiles ( ):以下实际编译( ):

std::stack<int, std::array<int, 12> > stack_of_months;

Until push is called.直到push被调用。

Is there some trick to easily wrap the std::array to support those methodes so it may be used in the stack?是否有一些技巧可以轻松包装std::array以支持这些方法,以便可以在堆栈中使用它?

You should be able to use Boost's static_vector :您应该能够使用 Boost 的static_vector

static_vector is a sequence container like with contiguous storage that can change in size, along with the static allocation , low overhead, and fixed capacity . static_vector是一个序列容器,类似于可以改变大小的连续存储,以及static 分配、低开销和固定容量

A working example:一个工作示例:

std::stack<int, boost::container::static_vector<int, 4>> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
// s.push(5); // would throw

@PaulMcKenzie And yes, it uses the stack as storage (if the object itself is stored in the stack, of course): @PaulMcKenzie 是的,它使用堆栈作为存储(当然,如果 object 本身存储在堆栈中):

The number of elements in a static_vector may vary dynamically up to a fixed capacity because elements are stored within the object itself similarly to an array. static_vector中的元素数量可以动态变化,直至达到固定容量,因为元素存储在 object 本身中,类似于数组。

Live demo: https://wandbox.org/permlink/Gi40FrB0yX8dIwfA .现场演示: https://wandbox.org/permlink/Gi40FrB0yX8dIwfA

It wouldn't be too hard to wrap a std::array into something usable by std::stack , you just need to keep track of how many elements have been pushed and popped:std::array包装成std::stack可用的东西并不难,您只需要跟踪有多少元素被推送和弹出:

#include <array>
#include <stack>
#include <iostream>

template < typename T, size_t Size >
struct static_vector
{
  using value_type = T;
  using reference = T&;
  using const_reference = const T &;
  using size_type = size_t;

  static_vector()
    : size( 0 )
  {
  }

  std::array< T, Size > data;
  size_t size;

  void push_back( const_reference value )
  {
    data.at( size ) = value;
    size++;
  }

  void pop_back()
  {
    if ( size == 0 )
    {
      throw std::runtime_error( "empty" );
    }
    size--;
  }

  const_reference back() const
  {
    if ( size == 0 )
    {
      throw std::runtime_error( "empty" );
    }
    return data[ size - 1 ];
  }

  reference back()
  {
    if ( size == 0 )
    {
      throw std::runtime_error( "empty" );
    }
    return data[ size - 1 ];
  }
};

int main()
{
  std::stack< int, static_vector< int, 4 > > stack;
  stack.push( 1 );
  std::cout << stack.top() << "\n";
  stack.push( 2 );
  std::cout << stack.top() << "\n";
  stack.pop();
  std::cout << stack.top() << "\n";
  stack.pop();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM