【问题标题】:How could I implement a vector of stack?如何实现堆栈向量?
【发布时间】:2015-01-04 19:03:13
【问题描述】:

我目前正在尝试使用堆栈向量实现一个类。该类应作为其他对象的堆栈工作,但在内部将数据分发到最大大小的不同堆栈。如果堆栈已满,则会创建一个新堆栈并将其推送到内部向量。

我目前的方法会产生错误:

prog.cpp: In instantiation of ‘SetOfStack<T>::SetOfStack(int) [with T = int]’:
prog.cpp:54:32:   required from here
prog.cpp:13:17: error: no matching function for call to ‘std::vector<std::stack<int, std::deque<int, std::allocator<int> > >, std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::push_back(std::stack<int, std::deque<int, std::allocator<int> > >*)’
                 stacks.push_back( new stack<T> );
                 ^
/usr/include/c++/4.8/bits/stl_vector.h:901:7: note:   no known conversion for argument 1 from ‘std::stack<int, std::deque<int, std::allocator<int> > >*’ to ‘const value_type& {aka const std::stack<int, std::deque<int, std::allocator<int> > >&}’
/usr/include/c++/4.8/bits/stl_vector.h:919:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::stack<int, std::deque<int, std::allocator<int> > >; _Alloc = std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > >; std::vector<_Tp, _Alloc>::value_type = std::stack<int, std::deque<int, std::allocator<int> > >]
       push_back(value_type&& __x)
       ^
/usr/include/c++/4.8/bits/stl_vector.h:919:7: note:   no known conversion for argument 1 from ‘std::stack<int, std::deque<int, std::allocator<int> > >*’ to ‘std::vector<std::stack<int, std::deque<int, std::allocator<int> > >, std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type&& {aka std::stack<int, std::deque<int, std::allocator<int> > >&&}’

prog.cpp: In instantiation of ‘void SetOfStack<T>::push(T) [with T = int]’:
prog.cpp:55:21:   required from here
prog.cpp:22:25: error: ‘__gnu_cxx::__alloc_traits<std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type’ has no member named ‘push_back’
                         stacks[current_stack].push_back(new stack<T>);
                         ^

prog.cpp: In instantiation of ‘T SetOfStack<T>::pop() [with T = int]’:
prog.cpp:56:27:   required from here
prog.cpp:34:25: error: ‘__gnu_cxx::__alloc_traits<std::allocator<std::stack<int, std::deque<int, std::allocator<int> > > > >::value_type’ has no member named ‘pop_back’
                         stacks[current_stack].pop_back();
                         ^

示例代码:

#include <iostream>
#include <stack>
#include <vector>
using namespace std;

template <class T>
class SetOfStack {
public:
    SetOfStack( int max_size ): current_stack(0), max_stack_size(max_size) {
            stacks.reserve(10);
            stacks.push_back( new stack<T> );
    }

    ~SetOfStack() {
            stacks.clear();
    }       

    void push( T value ) {
            stacks[current_stack].push(value);
            if(stacks[current_stack].size() > max_stack_size) {
                    stacks[current_stack].push_back(new stack<T>);
                    current_stack++;
                    if(current_stack % 10 == 0 && current_stack > stacks.size()) {
                            stacks.reserve(stacks.size() + 10);
                    }
            }
    }

    T pop() {
            T value = stacks[current_stack].top();
            stacks[current_stack].pop();

            if(stacks[current_stack].size() == 0 && current_stack != 0 ) {
                    stacks[current_stack].pop_back();
                    current_stack--;
            }
    }

    T popAt( int index ) {
            T value = stacks[index].top();
            stacks[index].pop();
    }

private:
    int current_stack;
    int max_stack_size;
    vector< stack<T> > stacks;
};


int main() {
    // Test code
    SetOfStack<int> s_o_s(3);
    s_o_s.push(1);
    cout << s_o_s.pop() << endl;
    return 0;
}

【问题讨论】:

  • 看起来你有一个大致的想法,有什么特别不适用于它吗?一对夫妇: 1. 在您的push 例程中,您正在检查size() &gt; max_stack_size,您可能想要size() &gt;= max_stack_size。 2. 由于您在push 例程中调用new stack&lt;T&gt;,因此当您执行current_stack-- 时,您需要在pop() 例程中调用delete。你还需要delete~SetOfStack()中的所有内容
  • 哦,你的pop() 好像你忘了return value;
  • 为什么要这样做?这不是基本上stackdeque 的支持下所做的(这是默认设置)吗?
  • 整个错误信息是什么?您只引用了最后一行。
  • 您应该在问题中发布错误,而不是在 cmets 中。您可以轻松编辑问题并添加错误

标签: c++ vector stack


【解决方案1】:

你得到的错误是这一行:

stacks.push_back( new stack<T> );

还有这一行:

stacks[current_stack].push_back(new stack<T>);

因为您已将堆栈声明为非指针:

vector< stack<T> > stacks;

所以您只想使用 stack&lt;T&gt;() 而不是 new stack&lt;T&gt; 另外std::stack没有push_back的功能,需要使用push代替。

stacks.push_back(stack<T>());
stacks[current_stack].push(value);

还有pop 而不是pop_back()

stacks[current_stack].pop();

此时,您不再需要我在原始评论中提到的删除,因为您似乎不打算致电 new

【讨论】:

  • 两者都应该是stacks.push_back(stack&lt;T&gt;()) - 在这两种情况下,向量都需要另一个堆栈。或者他们可以使用resize
  • 感谢您的回答。您可能是对的,但现在我收到一个错误,即 vector 没有足够的 stacks.push_back( new stack&lt;T&gt; ); 候选者。
  • @Koogle 对于vector,你使用push_back,但对于stack,你只使用push。 @Alan 纠正了我的答案,我对向量中的索引感到困惑并试图反击!
  • 好吧,想通了。 push_back 中的 new 是不必要的。
猜你喜欢
  • 1970-01-01
  • 2010-09-22
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 2020-04-11
  • 2010-11-27
  • 2013-12-11
  • 1970-01-01
相关资源
最近更新 更多