简体   繁体   English

C ++缩进重载ostream运算符

[英]C++ indent overloaded ostream operator

Let's say Ì have some class and added output functionality by overloading the left-shift operator: 假设通过重载左移运算符,Ì有一些类和添加的输出功能:

struct Foo
{
    int i = 1;
    std::string s = "hello";
};

auto& operator<<(std::ostream& os, Foo const& foo)
{
     os<<foo.i<<"\n";
     os<<foo.s<<"\n";
     return os;
}

What is a good way to indent the output? 缩进输出的好方法是什么?

Example: If I write 示例:如果我写

std::cout<<"    "<<Foo{}<<std::endl;

the output is: 输出是:

    1
hello

Obviously, hello is not indented. 显然, hello不是缩进的。 Is there an easy way to indent the whole output (and not just the first element)? 是否有一种简单的方法来缩进整个输出(而不仅仅是第一个元素)?

You're serializing the Foo object right? 你正在序列化Foo对象吗? So logically the serialized string of Foo is an implementation detail of Foo . 因此,逻辑上的序列化的字符串Foo是一个实现细节Foo You could write your own stream class or something along those lines but that is overengineering the problem. 可以编写自己的流类或类似的东西,但这是过度设计问题。

auto& operator<<(std::ostream& os, Foo const& foo)
{
    auto s = "\t" + std::to_string(foo.i) + "\n"
             "\t" + foo.s;
    return (os << s);
}

int main()
{
    std::cout << Foo{} << "\n";
}

You can use the standard library manipulator setw to set the width of a field, which often results in indenting the text. 您可以使用标准库操纵器setw来设置字段的宽度,这通常会导致缩进文本。 Here's how you use it: 以下是您使用它的方式:

cout << std::setw(10) << "Viola!" << std::endl;

This will print the word "Viola!" 这将打印“Viola!”这个词。 indented by 4 spaces. 缩进4个空格。 Why 4 spaces? 为什么4个空格? The parameter to setw() determines the entire width of the "field", which includes the 6 characters in "Viola!". setw()的参数确定“field”的整个宽度,其中包括“Viola!”中的6个字符。

By default, setw() will align the text to the right, but can be made to align left by using another manipulator left . 默认情况下, setw()将文本向右对齐,但可以通过使用另一个left操纵器使其对齐。 Here's an example: 这是一个例子:

cout << std::setw(10) << std::left << "Viola!" << std::endl;

This will output the string "Viola!" 这将输出字符串“Viola!” with no indentation, but with 4 spaces after it. 没有缩进,但后面有4个空格。

That should answer your original question about a good way to indent, and setw() is not just a good way, but the standard way. 这应该回答你关于缩进的好方法的原始问题,而setw()不仅是一种好方法,而且是标准方式。

The second question asks about how to have persistent indentation, and the answer is that there is not an easy way. 第二个问题是关于如何持续缩进,答案是没有一个简单的方法。 The easiest approach is to add the call to setw() (or whichever indentation method that you use) in each of the calls to cout. 最简单的方法是在每个cout调用中添加对setw()的调用(或者你使用的任何缩进方法)。

In addition to those answers, you should consider replacing the use of "\\n" in your calls to cout with a call to endl . 除了这些答案之外,你应该考虑通过调用endl来替换你对cout的调用中使用“\\ n”。 endl is the "end of line" manipulator, and makes your code work properly with any output stream. endl是“行尾”操纵器,使您的代码可以与任何输出流一起正常工作。 The code would look like this: 代码如下所示:

auto& operator<<(std::ostream& os, Foo const& foo)
{
     os << foo.i << std::endl;
     os << foo.s << std::endl;
     return os;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM