以编程方式输入新行
Enter a new line programmatically
我有一个带有文本的vector of std::string
,其中有几行。字符串(。这些线是矢量的元素。
我选择一个范围的数字,例如:0 and 2
,所选范围被删除,我在向量的开头插入一个新字符串,一个字符串(我删除的地方(。
But I would like
,当我输入一个字符串并在同一字符串中键入"">时,to see in the outputting result text which will consist of two lines.
为了获取带有spaces
的字符串,我使用std::getline()
:
std::cout << "Enter insreting text: ";
std::getline(std::cin >> std::ws, text);
在控制台模式下:
Enter insreting text: hello n Bye
我的愿望结果应该是
你好
再见
可能是我使用 std::getline()
来获取字符串是不对的。请问有什么提示吗?
正如我的评论中提到的,如果您输入,std::getline()
捕获的文本
hello n Bye
是
"hello \n Bye"
显示为文字。
要输出
hello
Bye
您需要将"\n"
的出现次数替换为 "n"
。
@Remy已经在他的回答中发布了如何做到这一点的代码。
std::string text;
std::cout << "Enter inserting text: ";
std::getline(std::cin >> std::ws, text);
std::string::size_type pos = text.find("\n");
while (pos != std::string::npos)
{
text.replace(pos, 2, "n");
pos = text.find("\n", pos+1);
}
std::cout << text;
现场演示
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- Arduino Nano:A4988 使用串行输入时通过定时器进行步进控制不稳定
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 使用makefile中的命令行输入
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- 为什么当测试用例数为 1 时,我的程序不输入而是打印新行?
- 以编程方式输入新行
- c++输入,按至新行
- 如何在不创建新行的情况下使用cin输入变量