在 C++ 的字符串中,我们可以使用换行符 "\n" 来表示换行。换行符在字符串中是一个特殊的字符,用于将光标移到下一行的开头。
例如,假设我们要将字符串 "Hello World" 和 "How are you?" 分别输出到两行中,我们可以使用以下代码:
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello World";
std::string str2 = "How are you?";
std::cout << str1 << "\n" << str2 << std::endl;
return 0;
在上面的代码中,我们使用了 "\n" 来表示换行,将两个字符串分别输出到了两行中。注意,我们使用 std::endl 来在每个字符串的末尾输出一个换行符,以确保输出缓冲区被清空并刷新到屏幕上。
另外,有些操作系统使用不同的换行符,比如 Windows 使用 "\r\n",而 Unix/Linux 使用 "\n"。为了保持代码的跨平台性,建议使用 C++ 标准库中的 std::endl 来输出换行符。