📜  在 C++ 中多行打印输出

📅  最后修改于: 2022-05-13 01:55:21.796000             🧑  作者: Mango

在 C++ 中多行打印输出

本文重点讨论如何使用 cout 进行多行打印。这可以使用以下两种方法中的任何一种轻松完成:

  • 使用 endl。
  • 使用\n。

让我们详细讨论这些方法。

使用 endl

endl 语句可用于在单个 cout 语句中打印多行字符串。下面是显示相同内容的 C++ 程序:

C++
// C++ program to show endl statement
// can be used to print the multi-line
// string in a single cout statement
#include 
using namespace std;
  
// Driver code
int main() 
{
    cout <<" GeeksforGeeks is best" 
           " platform to learn " << endl << " It is used by" 
           " students to gain knowledge" <<
           endl << " It is really helpful";
    return 0;
}


C++
// C++ program to show '\n' can be
// used instead of endl to print 
// multi-line strings
#include 
using namespace std;
  
// Driver code 
int main() 
{
    cout << " GeeksforGeeks is best" 
            " platform to learn \n It" 
            " is used by students to" 
            " gain knowledge \n It is" 
            " really helpful";
    return 0;
}


输出
GeeksforGeeks is best platform to learn 
 It is used by students to gain knowledge
 It is really helpful

使用“\n”

'\n' 可以用来代替 endl 来打印多行字符串。下面是实现上述方法的 C++ 程序:

C++

// C++ program to show '\n' can be
// used instead of endl to print 
// multi-line strings
#include 
using namespace std;
  
// Driver code 
int main() 
{
    cout << " GeeksforGeeks is best" 
            " platform to learn \n It" 
            " is used by students to" 
            " gain knowledge \n It is" 
            " really helpful";
    return 0;
}
输出
GeeksforGeeks is best platform to learn 
 It is used by students to gain knowledge 
 It is really helpful