(C++) 在堆栈中使用向量作为包装器
(C++) using a vector as a wrapper in a stack
编辑:回答了原始问题。不过,相关问题并不像是在制作一个新的 post。为什么我无法使用堆栈的 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.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();}
^
代码:
void HCTree::encode(byte symbol, BitOutputStream& out) const
{
HCNode* temp;
temp = leaves[symbol];//store leaf node containing symbol into temp
/* traverse to the top of the tree */
while(temp->p != NULL)
{
/* record path we take to parent into a stack */
if(temp == temp->p->c0)//if temp is the c0 child
encoding.push(0);
else//temp is the c1 child
encoding.push(1);
temp = temp->p;//move up to temp's parent and repeat
}
/* write bits to buffer */
out.writeBit(encoding.pop());
}
来自HCTree.hpp的相关行:
stack<int,std::vector<int>> encoding;
是否有关于使用向量阻止我使用 push() 和 pop() 函数调用的问题?
----原文post----:
我正在尝试创建一个堆栈,它使用向量作为包装器在 C++ 中存储整数,就像在此处看到的示例中所做的那样:http://www.cplusplus.com/reference/stack/stack/stack/
我需要一个存储整数的堆栈,所以我有代码:
std::vector<int> wrapper;
stack<int,std::vector<int>> encoding (wrapper);
我收到以下错误:
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);
如何修复我的实现?我需要创建一个最初的 空 堆栈来在我回溯二叉树时将 1 和 0 压入,以便稍后弹出以重建所采用的路径。
默认为空堆栈。您只需要:
#include <stack>
#include <vector>
std::stack<int, std::vector<int> > encoding;
此外,cplusplus.com 不是一个很好的来源。它有更多的广告和更少的编辑。避开它。
I need to create an initially empty stack to push 1's and 0's onto as I backtrace a binary tree in order to later pop off to rebuild the path that was taken.
为什么不 std::stack< bool, std::vector< bool > >
?请注意 std::vector< bool >
具有最佳存储特性。
第二个问题
(请不要添加问题。打开新问题是免费的。)
您不能在标记为 const
的成员函数内更改 encoding
,因为该上下文使数据成员的行为与 const
相同。有几种解决方案:
- 让
encode
函数不是 const
,因为它有意义地改变了 HCTree
对象的状态。
- 不要让
encoding
成为 HCTree
的成员。将它放在其他一些对象中或要求它由用户提供。
- 声明
encoding
为 mutable
。这似乎不合适。 mutable
成员的状态通常不应该被观察到,除了性能提升等副作用。
编辑:回答了原始问题。不过,相关问题并不像是在制作一个新的 post。为什么我无法使用堆栈的 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.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();}
^
代码:
void HCTree::encode(byte symbol, BitOutputStream& out) const
{
HCNode* temp;
temp = leaves[symbol];//store leaf node containing symbol into temp
/* traverse to the top of the tree */
while(temp->p != NULL)
{
/* record path we take to parent into a stack */
if(temp == temp->p->c0)//if temp is the c0 child
encoding.push(0);
else//temp is the c1 child
encoding.push(1);
temp = temp->p;//move up to temp's parent and repeat
}
/* write bits to buffer */
out.writeBit(encoding.pop());
}
来自HCTree.hpp的相关行:
stack<int,std::vector<int>> encoding;
是否有关于使用向量阻止我使用 push() 和 pop() 函数调用的问题?
----原文post----: 我正在尝试创建一个堆栈,它使用向量作为包装器在 C++ 中存储整数,就像在此处看到的示例中所做的那样:http://www.cplusplus.com/reference/stack/stack/stack/
我需要一个存储整数的堆栈,所以我有代码:
std::vector<int> wrapper;
stack<int,std::vector<int>> encoding (wrapper);
我收到以下错误:
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);
如何修复我的实现?我需要创建一个最初的 空 堆栈来在我回溯二叉树时将 1 和 0 压入,以便稍后弹出以重建所采用的路径。
默认为空堆栈。您只需要:
#include <stack>
#include <vector>
std::stack<int, std::vector<int> > encoding;
此外,cplusplus.com 不是一个很好的来源。它有更多的广告和更少的编辑。避开它。
I need to create an initially empty stack to push 1's and 0's onto as I backtrace a binary tree in order to later pop off to rebuild the path that was taken.
为什么不 std::stack< bool, std::vector< bool > >
?请注意 std::vector< bool >
具有最佳存储特性。
第二个问题
(请不要添加问题。打开新问题是免费的。)
您不能在标记为 const
的成员函数内更改 encoding
,因为该上下文使数据成员的行为与 const
相同。有几种解决方案:
- 让
encode
函数不是const
,因为它有意义地改变了HCTree
对象的状态。 - 不要让
encoding
成为HCTree
的成员。将它放在其他一些对象中或要求它由用户提供。 - 声明
encoding
为mutable
。这似乎不合适。mutable
成员的状态通常不应该被观察到,除了性能提升等副作用。