重载运算符<<用于 std::stack
overload operator<< for std::stack
代码优先:
//. cpp文件template <typename T>
ostream &operator<<(ostream &os, stack<T> &st)
{
while(! st.empty()) {
os << st.top() << " ";
st.pop();
}
return os;
}
template <typename T>
void stack_sorter(stack<T> &st)
{
cout << st << endl; //no output, st is empty?
//...
}
int main()
{
stack<int> s;
s.push(4);
s.push(3);
s.push(5);
cout << s << endl; //ok
stack_sorter(s);
}
输出:5 3 4 //this was the output in main, not in stack_sorter
//empty line, but s is not empty, why?
问题:
和我在main中所做的一样,我将s传递给stack_soter(),但是在stack_sorter()中没有得到输出。怎么了?
在您的operator <<
中,您弹出所有值以输出它们。所以在cout << s << endl;
之后,你的堆栈被清空!
一种可能是不通过引用传递,而是通过堆栈的副本传递。但是,这也会复制其中的所有对象。
template <typename T>
ostream &operator<<(ostream &os, stack<T> st) //not &st
{
while(! st.empty()) {
os << st.top() << " ";
st.pop();
}
return os;
}
正如其他人指出的那样,pop_back
使您的输出操作具有破坏性。没有办法读取堆栈的内容而不清空它,这就是纯堆栈的本质。
同样,为标准类型重载操作符也是非常糟糕的做法。由于名称查找的工作方式(ADL,即语言查找要调用的重载函数的方式),它的重载函数应该与它们重载的类型在同一个名称空间中。由于不能将函数放在std::
中,所以下一个最佳选择是全局命名空间,但这会对全局命名空间造成污染。
但是这个问题是可以解决的!幸运的是,stack
提供了继承。底层容器可以被派生类访问,它被命名为c
。
template< typename value_arg, typename container_arg = std::deque< value_arg > >
struct printable_stack : std::stack< value_arg, container_arg > {
// provide constructors :v( . This is better supported in C++11.
printable_stack( container_arg const &in = container_arg() )
: std::stack< value_arg, container_arg >( in ) {}
friend std::ostream &operator<<
( std::ostream &s, printable_stack const &o ) {
for ( typename container_arg::iterator i = this->c.begin();
i != this->c.end(); ++ i ) {
s << * i << ' ';
}
}
};
另外,有一个专用的类允许您提供一个额外的成员来保存分隔符字符串,所以它可以是一个简单的空格字符以外的东西。
在ostream &operator<<(ostream &os, stack<T> &st)
的循环中调用st.pop()
将清空堆栈
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- std::stack 是连续的吗?
- 为什么我会收到"Run-Time Check Failure #2 - Stack around the variable 'pr' was corrupted"错误?
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 堆叠协程 + gdb = "previous frame inner to this frame (corrupt stack)?"
- 生成质数的程序,错误:"Stack overflow"
- 如何在 x64 上"stack oveflow"例外
- 为什么此代码中显示"*** stack smashing detected ***: <unknown> terminated Aborted (core dumped) "错误?
- Qt Creator 在执行步骤 "make" 时出现编译错误,-fno-stack-limit
- 无法在基于 DFS 的任务排序程序中填充"stack"
- std::stack的奇怪行为,pop()返回相同的值
- 如何显式调用运算符<<
- 使用双LL在C++中实现Stack失败,出现异常"EXC_BAD_ACCESS(代码=2,地址=0x7fff5
- 如何获得"基于堆栈"的std::stack?(具有固定的最大尺寸)
- 为什么我会收到"invalid conversion from 'Queue*/Stack*' to 'int'"错误消息?
- 如何修复初始化后'stack around variable was corrupted.'变量未更改为分配的值
- Ctypes: OSError: exception: stack overflow