我怎样才能实现堆栈的向量
How could I implement a vector of stack?
我目前正在尝试实现一个具有堆栈向量的类。该类应作为其他对象的堆栈工作,但将数据分发到内部最大大小的不同堆栈。如果堆栈已满,则会创建一个新堆栈并将其推送到内部向量。
我目前的方法会产生错误:
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
相关文章:
- 对象接收堆栈溢出异常 c++ 的排序向量
- 对象向量是否在堆或堆栈上分配C++?
- 在C++中使用队列和堆栈对 2 个向量进行加减
- 是否可以在堆栈上创建向量
- 堆栈在函数中弹出仍然显示在主函数中。调用不应该通过引用给定的向量吗
- 附加到具有非动态分配堆栈的向量
- 如何正确打印堆栈的向量
- 从分配在堆栈上的对象指针的向量中删除元素
- 链表与动态数组用于使用向量类实现堆栈
- 向量的向量,堆与堆栈(C++)
- 制作数组堆栈和向量的二维数组
- 将向量从堆栈复制到堆上
- 堆栈分配的向量如何在 C++ 中扩展
- 存储在对象向量上的基于堆栈的指针具有很长的使用寿命
- C++向量的堆栈分配
- 在C++中,是在堆栈或堆上的函数中创建的向量或映射
- 为什么存储在 STL 向量中的堆栈分配元素C++即使超出范围也仍然处于活动状态
- 我怎样才能实现堆栈的向量
- 为什么这不会导致堆栈溢出?其中是存储的全局向量(堆栈或堆)
- 堆栈上有成员的向量