Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 Book 类,我想在 C++ 中实现一个 toString 方法,使用 setw(x),以正确模式格式化。
我想打印这样的东西:
> cout<<setw(10)<<book.title<<setw(10)<<book.author;
但在 toString 方法中:
string toString(){ string buff; .... return buff; }
谢谢!
使用std::ostringstream,例如:
std::ostringstream
#include <sstream> string toString() const { std::ostringstream buff; buff << std::setw(10) << title << std::setw(10) << author; return buff.str(); }
cout << book.toString()