简体   繁体   English

将std :: stack复制到std :: vector中

[英]Copy std::stack into an std::vector

Is the following code guaranteed by the standard to work(assuming st is not empty)? 标准是否保证以下代码可以工作(假设st不为空)?

#include <vector>
#include <stack>
int main()
{
   extern std::stack<int, std::vector<int> > st;
   int* end   = &st.top() + 1;
   int* begin = end - st.size();
   std::vector<int> stack_contents(begin, end);
}

Yes. 是。

std::stack is just a container adapter. std::stack只是一个容器适配器。

You can see that .top() is actually (§23.3.5.3.1) 你可以看到.top .top()实际上是(§23.3.5.3.1)

reference top() { return c.back(); }

Where c is the container, which in this case is a std::vector 其中c是容器,在本例中是std::vector

Which means that your code is basically translated into: 这意味着您的代码基本上被翻译成:

   extern std::vector<int> st;
   int* end   = &st.back() + 1;
   int* begin = end - st.size();
   std::vector<int> stack_contents(begin, end);

And as std::vector is guaranteed to be continuous there should be no problem. 并且由于std::vector保证是连续的,所以应该没有问题。

However, that does not mean that this is a good idea. 但是,这并不意味着这是一个好主意。 If you need to use "hacks" like this it is generally an indicator of bad design. 如果你需要使用这样的“黑客”,它通常是糟糕设计的一个指标。 You probably want to use std::vector from the beginning. 你可能想从头开始使用std::vector

Only std::vector is guaranteed by C++03 to have contiguous elements ( 23.4.1 ). C ++ 03只保证std::vector具有连续元素( 23.4.1 )。 In C++1x this will be extended to std::string as well ( defect #530 ). 在C ++ 1x中,这也将扩展到std::string缺陷#530 )。

Yes, it's guaranteed. 是的,这是有保证的。 Vectors are guaranteed to use contiguous storage, so your code will work. 保证向量使用连续存储,因此您的代码将起作用。 It's a bit cludgy though - and if someone changes the underlying container type of the stack, your code will continue to compile without errors, yet the runtime behaviour will be broken. 虽然它有点笨拙 - 如果有人更改了堆栈的基础容器类型,您的代码将继续编译而不会出现错误,但运行时行为将被破坏。

I don't have a reference to the standard to back this up unfortunately, but there aren't many ways in which it could go wrong I guess: 遗憾的是,我没有参考标准来支持这个问题,但我认为没有多少方法可以解决这个问题:

  • Specifying std::vector<int> as the container type means that the elements must be stored in a std::vector<int> . std::vector<int>指定为容器类型意味着元素必须存储在std::vector<int>
  • st.top() must return a reference to an element in the underlying container (ie an element in the std::vector<int> . Since the requirements on the container are that it supports back() , push_back() and pop_back() , we can reasonably assume that top() returns a reference to the last element in the vector. st.top()必须返回对底层容器中元素的引用(即std::vector<int>的元素。由于对容器的要求是它支持back()push_back()pop_back() ,我们可以合理地假设top()返回对向量中最后一个元素的引用。
  • end therefore points to one past the last element. 因此, end指向最后一个元素。
  • start therefore points to the beginning. 因此start指向开头。

Conclusion: Unless the assumption was wrong, it must work. 结论:除非假设是错误的,否则它必须有效。

EDIT: And given the other answer's reference to the standard, the assumption is correct, so it works. 编辑:并给出其他答案的标准参考,假设是正确的,所以它的工作原理。

According to this page , std::stack uses a container class to store elements. 根据这个页面std::stack使用容器类来存储元素。

I guess what you suggest works only if the containter store its elements in a linear way ( std::vector ). 我猜你的建议只有当包含者以线性方式( std::vector )存储其元素时才有效。

As a default, std::stack uses a std::deque which, as far as I know, doesn't meet this requirement. 默认情况下, std::stack使用std::deque ,据我所知,它不符合此要求。 But If you specify a std::vector as a container class, I can't see a reason why it shoudln't work. 但是如果你将std::vector指定为容器类,我就看不出它为什么不起作用的原因了。

Edit: initial statement redacted, the standard actually does provide a full definition for the stack adaptor, nothing left to implentors. 编辑:初始语句编辑,标准实际上确实提供了堆栈适配器的完整定义,没有任何留给实现者。 see top answer. 看到最佳答案。

You want a container that has a push and pop method and allows you to inspect elements anywhere in the container and uses a std::vector for storage. 您需要一个具有push和pop方法的容器,并允许您检查容器中任何位置的元素,并使用std::vector进行存储。 There is such a container in the standard template library 标准模板库中有这样的容器

it is called std::vector . 它被称为std::vector

Use std::stack only for bondage purposes 仅将std::stack用于绑定目的

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

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