c++ - \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";
解决方案
是的,你可以这样:
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. ...
)";
推荐阅读
- arrays - 找到四倍数
- c# - 使用 c# 在 monogame 的其他类中访问 GraphicsDeviceManager
- r - 在每个记录/行级别在 R 中创建一个计算字段
- wordpress - 按上个月前发布的帖子数获取用户列表
- python-3.x - 无法使用 TensorFlow 旧版本和新版本
- parameters - 通过名称传递评估变量
- ruby - RVM 不保留 ruby 版本
- json - 使用 bash 和 AWS CLI 解除关联并释放来自所有区域的所有弹性 IP 地址的脚本
- angular - 我该如何解决这个问题?serve 命令需要在 Angular 项目中运行,但找不到项目定义
- python - 模块/包导入期间发生了什么