C++ (C+;+;)使用向量作为堆栈中的包装器
编辑:原始问题已回答。不过,相关问题并没有让人觉得它是在写一篇新文章。为什么我不能使用堆栈的push()和pop()函数调用?以下是错误:C++ (C+;+;)使用向量作为堆栈中的包装器,c++,vector,stack,C++,Vector,Stack,编辑:原始问题已回答。不过,相关问题并没有让人觉得它是在写一篇新文章。为什么我不能使用堆栈的push()和pop()函数调用?以下是错误: HCTree.cpp:65:16: error: no matching member function for call to 'push' encoding.push(0); ~~~~~~~~~^~~~ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoo
HCTree.cpp:65:16: error: no matching member function for call to 'push'
encoding.push(0);
~~~~~~~~~^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:197:10: note:
candidate function not viable: 'this' argument has type 'const stack<int,
std::vector<int> >', but method is not marked const
void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:194:10: note:
candidate function not viable: 'this' argument has type 'const stack<int,
std::vector<int> >', but method is not marked const
void push(const value_type& __v) {c.push_back(__v);}
^
HCTree.cpp:67:16: error: no matching member function for call to 'push'
encoding.push(1);
~~~~~~~~~^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:197:10: note:
candidate function not viable: 'this' argument has type 'const stack<int,
std::vector<int> >', but method is not marked const
void push(value_type&& __v) {c.push_back(_VSTD::move(__v));}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:194:10: note:
candidate function not viable: 'this' argument has type 'const stack<int,
std::vector<int> >', but method is not marked const
void push(const value_type& __v) {c.push_back(__v);}
^
HCTree.cpp:73:16: error: member function 'pop' not viable: 'this' argument has type 'const
stack<int, std::vector<int> >', but function is not marked const
out.writeBit(encoding.pop());
^~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/stack:206:10: note:
'pop' declared here
void pop() {c.pop_back();}
^
来自HCTree.hpp的相关行:
stack<int,std::vector<int>> encoding;
堆栈编码;
使用向量会阻止我使用push()和pop()函数调用吗
----原稿--:
我试图创建一个堆栈,它用向量作为包装器来存储C++中的int,如在这里看到的例子所做的:
我需要一个存储int的堆栈,所以我有代码:
std::vector<int> wrapper;
stack<int,std::vector<int>> encoding (wrapper);
std::向量包装器;
堆栈编码(包装器);
我得到以下错误:
Compiling: compress.cpp -> build/compress.o
In file included from compress.cpp:19:
./HCTree.hpp:35:43: error: unknown type name 'wrapper'
stack<int,std::vector<int>> encoding (wrapper);
编译:compress.cpp->build/compress.o
在compress.cpp:19中包含的文件中:
./HCTree.hpp:35:43:错误:未知类型名称“wrapper”
堆栈编码(包装器);
如何修复我的实现?我需要创建一个初始空堆栈,在回溯二叉树时将1和0推到上面,以便稍后弹出以重建所采用的路径。默认为空堆栈。您所需要的只是:
#include <stack>
#include <vector>
std::stack<int, std::vector<int> > encoding;
#包括
#包括
std::堆栈编码;
另外,cplusplus.com不是一个很好的源代码。它有更多的广告和更少的编辑。避免它
我需要创建一个最初为空的堆栈,在回溯二叉树时将1和0推到堆栈上,以便稍后弹出以重建所采用的路径
为什么不std::stack
?请注意,std::vector
具有最佳的存储特性
第二个问题 (请不要添加问题。打开新问题是免费的。) 不能在标记为
const
的成员函数中更改编码
,因为该上下文使数据成员表现为const
。有几种解决方案:
encode
函数不是const
,因为它有意义地改变了HCTree
对象的状态编码
成为HCTree
的成员。将其放入其他对象或要求用户提供编码
声明为可变
。这似乎不合适。除了诸如性能改进等副作用外,可变成员的状态通常不应被观察到
无法重现:您的错误还声称至少有19行代码,但您只显示了2行……您确实应该发布一个不同的问题(如果您觉得下面的答案有用,请接受)。
#include <stack>
#include <vector>
std::stack<int, std::vector<int> > encoding;