,但我怎么能打印队列中的所有元素“my_queue_of_queues”?
选项1使用operator<<
代替print
。
template<typename T>
std::ostream& operator<<(std::ostream& out, queue<T> const& q)
{
// Don't modify the input.
// Create a copy and modify the copy.
queue<T> copy(q);
while(!copy.empty())
{
T x;
copy.pop(x);
out << x << " ";
}
return out;
}
选项2更新print
并调用operator<<
功能重新直接执行。
template<typename T>
void queue<T>::print()
{
cout << *this;
}
选项3更新print
并调用operator<<
功能重新直接执行,而且通过一个std::ostream
到print
。不要假设cout
在print
。
template<typename T>
void queue<T>::print(std::ostream& out)
{
out << *this;
}