我怎样才能实现堆栈向量?

How could I implement a vector of stack?

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

我当前的方法产生错误:

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;
}

您遇到的错误是这一行:

stacks.push_back( new stack<T> );

以及这一行:

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

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

vector< stack<T> > stacks;

所以你只想使用 stack<T>() 而不是 new stack<T> 此外,std::stack 没有函数 push_back,您需要使用 push 代替。

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

以及pop而不是pop_back()

stacks[current_stack].pop();

此时,您不再需要我在原始评论中提到的删除,因为看起来您并不打算调用 new