C++ 如何在C++中输出文件中的多个空格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17523407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:15:57  来源:igfitidea点击:

how to multiple space lines in the output file in C++

c++

提问by H'H

I am wondering if there is any short command in C++ for adding multiple space lines in the output? (I know about "endl" and "\n")

我想知道 C++ 中是否有任何短命令可以在输出中添加多个空格行?(我知道“endl”和“\n”)

thanks for any help in advance.

感谢您提前提供任何帮助。

回答by log0

No there are no special facilities for adding multiple space lines. you can do this:

不,没有用于添加多条空间线的特殊设施。你可以这样做:

std::cout << "\n\n\n\n\n";

or this

或这个

for (int i = 0; i < 5; ++i)
  std::cout << "\n";

or implement your own operator*

或实现你自己的 operator*

std::string operator*(std::string const &s, std::size_t n)
{
  std::string r;
  r.reserve(n * s.size());
  for (std::size_t i = 0; i < n; ++i)
    r += s;
  return r;
}

std::cout << (std::string("\n") * 5);

finally, recommended solution:

最后,推荐的解决方案:

std::cout << std::string( 5, '\n' );

回答by Alex

You can write your own manipulator, that can insert multiple newlines at a time. Let's call it mendl(multiple endl):

您可以编写自己的manipulator,它可以一次插入多个换行符。让我们称之为mendl(多个 endl):

class mendl
{
public:
    explicit mendl(unsigned int i) : n(i) {}
private:
    unsigned int n;

    template <class charT, class Traits>
    friend basic_ostream<charT,Traits>& operator<< (
                                         basic_ostream<charT,Traits>& os,
                                         const mendl& w)
    {
        // the manipulation: insert end-of-line characters and flush
        for (unsigned int i=0; i<w.n; i++)
            os << '\n';
        os.flush();
        return os;
    }
};

Usage is:

用法是:

cout << "dfsdf" << mendl(4);

回答by yzt

You can always construct a string that has as many new-line characters (technically, LF) as you want, like so:

您始终可以构建一个包含任意数量换行符(技术上为 LF)的字符串,如下所示:

cout << "Whatever..." << string(42, '\n');

This will output 42 new lines after the "Whatever...". Another way, of course, is to define a new type (called e.g. mendlas another one of the answersdoes.)

这将在“Whatever...”之后输出 42 行新行。另一种方法,当然是定义一个新的类型(如叫mendl作为答案的另外一个呢。)

There are a multitude of things you can do, but the simplest and most straightforward is to use the above std::stringconstructor. But, you mightneed to flushyour stream, depending on your use case.

你可以做很多事情,但最简单和最直接的是使用上面的std::string构造函数。但是,您可能需要flush流,具体取决于您的用例。

回答by banarun

while(k--)cout<<"\n"; // k is number of lines you wanted

回答by MichaelH

No, the standard library contains I and O methods but what's in the data stream is entirely up to you.

不,标准库包含 I 和 O 方法,但数据流中的内容完全取决于您。

endl does half of what you asked for. Calling it twice would get what you want. Alternatively you could define a endl2x that puts out 2 newlines or have one that takes a parameter defining how many to emit

endl 做了你要求的一半。调用它两次会得到你想要的。或者,您可以定义一个 endl2x 来输出 2 个换行符,或者使用一个参数来定义要发出多少个换行符

回答by Abhishek Dixit

you could try writing a function taking output stream and no of new lines to print returning the output stream

您可以尝试编写一个函数来获取输出流并且没有新行来打印返回输出流

回答by rbtLong

You may consider defining a constant variable if you are using a constant amount of newlines

如果您使用恒定数量的换行符,您可以考虑定义一个常量变量

for example,

例如,

const char nl5[] = "\n\n\n\n\n";

you may use it in the context of a cout like std::endl.

你可以在像 std::endl 这样的 cout 的上下文中使用它。

here is the entire code . . .

这是整个代码。. .

#include <iostream>

int main()
{
    using namespace std;
    const char nl5[] = "\n\n\n\n\n";
    cout << "ln1" << nl5 << endl;
    cout << "ln2" << nl5 << endl;
}