首页 > 解决方案 > \n 在我按下回车键后不工作

问题描述

我正在cout用 C++ 编写一个语句,但是该语句非常大,所以我按 Enter 键以便我可以从下一行开始(不想在一行中编写完整的长语句)。它工作正常,但是如果\n(新行)是按回车后的第一个字符,因为您可以看到第二行代码它不起作用。所以我只想问有没有办法在敲回车后从下一行(继续上一行代码)开始你的代码。

cout<<"\nChoose the operation you want to perform :
\n1. To insert a node in the BST and in the tree \n2";

标签: c++enter

解决方案


是的,你可以这样:

std::cout << "\nChoose the operation you want to perform:\n"
             "1. To insert a node in the BST and in the tree\n"
             "2. ...\n";

您不能在一行上包含一个不以 a 结尾的"字符串,但是将连续两个正确终止的字符串连接起来。就这样"foo" "bar"变成了"foobar"。有"foo""bar"在不同的行很好。

正如其他人所提到的,C++11 支持原始字符串文字,它确实允许字符串分布在多行中,并且避免了编写\n

std::cout << R"(
Choose the operation you want to perform:
1. To insert a node in the BST and in the tree
2. ...
)";

推荐阅读