#include <iostream>
using namespace std;
int main() {
// 使用换行符 \n 进行换行
cout << "Hello, World!\nThis is a new line." << endl;
// 使用 endl 进行换行并刷新输出缓冲区
cout << "Another line" << endl;
// 使用 put 函数进行换行
cout.put('\n');
cout << "Yet another line";
return 0;
}
\n
:这是最常见的换行符,它会在输出字符串时插入一个新行。例如,cout << "Hello, World!\nThis is a new line.";
会先输出 Hello, World!
然后换行再输出 This is a new line.
。endl
:除了换行外,endl
还会刷新输出缓冲区,确保所有输出立即显示在终端上。例如,cout << "Another line" << endl;
会在输出 Another line
后换行并刷新缓冲区。put
函数:可以使用 put
函数直接输出单个字符,包括换行符 \n
。例如,cout.put('\n');
会在当前位置插入一个新行。希望这些示例能帮助你理解 C++ 中的换行符用法。
上一篇:c++ regex
下一篇:c++ 关键字
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站