[英]How to initialize std stack with std vector?
I need to put an std::vector
into an std::stack
. 我需要将
std::vector
放入std::stack
。
Here is my method so far(I am building a card game) : 这是我到目前为止的方法(我正在建立纸牌游戏):
void CardStack::initializeCardStack(std::vector<Card> & p_cardVector) {
m_cardStack = std::stack<Card>();
//code that should initialize m_cardStack with p_cardVector
}
Note : I cannot change my method signature because it is a imposed by a teacher... 注意:我无法更改我的方法签名,因为它是由老师强加的...
Do I have to iterate over the whole vector ? 我是否必须迭代整个向量? What is the most efficient way to do this ?
最有效的方法是什么? The documentation .
文档 。
I have tried Jens answer but it didn't work. 我试过Jens答案,但它没有用。
std::stack
doesn't have a constructor which accepts iterators, so you could construct a temporary deque
and initialize the stack with this: std::stack
没有接受迭代器的构造函数,因此你可以构造一个临时的deque
并用这个初始化堆栈:
void ClassName::initializeStack(std::vector<AnotherClass> const& v) {
m_stackAttribute = std::stack<AnotherClass>( std::stack<AnotherClass>::container_type(v.begin(), v.end()) );
}
However, this copies each element into the container. 但是,这会将每个元素复制到容器中。 For maximum efficiency, you should also use move-semantics to eliminate copies
为了获得最大效率,您还应该使用移动语义来消除副本
void ClassName::initializeStack(std::vector<AnotherClass>&& v) {
std::stack<AnotherClass>::container_type tmp( std::make_move_iterator(v.begin()), std::make_move_iterator( v.end() ));
m_stackAttribute = std::stack<AnotherClass>( std::move(tmp) );
}
The most efficient way is not using an std::stack
at all and just use a std::vector
or even better for this use a std::deque
. 最有效的方法是根本不使用
std::stack
,只使用std::vector
,甚至更好地使用std::deque
。
I've seen and written a lot of C++ code (a lot) but I've yet to find any use for the stack
stuff (any meaningful use, that is). 我已经看过并编写了很多C ++代码(很多)但是我还没有找到任何用于
stack
东西(任何有意义的用途,即)。 It would be different if the underlying container could be changed or having its container type determined at runtime, but this is not the case. 如果可以更改底层容器或在运行时确定其容器类型,则会有所不同,但事实并非如此。
To copy the elements from an std::vector
into a std::deque
you can just use 要将
std::vector
的元素复制到std::deque
您只需使用即可
std::deque<T> stack(vec.begin(), vec.end());
This will allow the implementation to use the most efficient way to copy the elements. 这将允许实现使用最有效的方式来复制元素。
To explicitly answer your question: yes, the only way to put elements in a stack is to push them in with a loop. 要明确回答你的问题:是的,将元素放入堆栈的唯一方法是使用循环将它们推入。 This is not efficient but the stack interface doesn't define anything else.
这样效率不高,但堆栈接口没有定义任何其他内容。 However who wrote code accepting an
std::stack
parameter should be fired (unless s/he promises that it will never happen again) and its code reverted to something more sensible: you would get the same (absence of) "flexibility" but a better interface. 然而,编写接受
std::stack
参数的代码的人应该被解雇(除非他/她承诺它永远不会再发生)并且它的代码恢复到更合理的东西:你会得到相同的(没有)“灵活性”但是更好的界面。
The design problem of stack
is that it's parametrized on the underlying container type while instead (to have any meaning) should have been parametrized on the contained element type and receving in the constructor a container for that type (thus hiding the container type). stack
的设计问题是它在底层容器类型上进行参数化,而相反(具有任何含义)应该在包含的元素类型上进行参数化,并在构造函数中接收该类型的容器(从而隐藏容器类型)。 In its present form is basically useless. 目前的形式基本没用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.