C++ cout 在擦除换行符后打印字符串不正确
C++ cout prints string incorrectly after erasing newline characters?
我有一个多行字符串:
123 456 7
8910
例如。我删除了函数int clean(string& text);
中的所有换行符 '',并使用 cout
在主中打印字符串。结果是:
8910456 7
我知道我的函数工作"正确",因为str.substr(0, 5)
的结果是:
123 4
此外,我正确地通过cout
打印了另一个多行字符串,因此cout
正常工作。还包括string
和iostream
图书馆。
int clean(string& text) {
//...
text.erase(std::remove(text.begin(), text.end(), 'n'), text.end());
return 0;
}
int main() {
string t1;
string name1 = "../test.txt";
try {
t1 = readBlock(name1);
cout << t1 << endl;
} catch(exception &e) {
cout << e.what();
}
cout << "Size: " << t1.size() << endl;
clean(t1); //Here
cout << t1 << "n";
return 0;
}
其他方法,擦除换行符,例如将除换行符以外的每个字符添加到新字符串中,会产生相同的结果。我做错了什么?
你的行尾(我从你的结果推断(实际上是rn
的,而不仅仅是n
。 这是一个回车字节,后跟一个换行字节。
然后,您将第一行输出到cout
,将字符串打印到控制台。 然后,您点击回车 ( r
( 字节,当输出到控制台时,将光标放回行的开头。 然后,第二行输出到控制台,但仍在同一行上,并打印在现有文本的顶部。
相关文章:
- C++ |<<运算符重载和打印字符串
- 反向打印字符串
- C++ 用于在数组中打印字符串的随机数
- C 不打印字符串
- 如何在没有同步的情况下使用多个线程(2、4,8、16 个线程)在循环(10,100、1000 个周期)中打印字符串?
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- 使用带有 std::cout 的单引号打印字符串实际上打印了数字
- 使用迭代从链表向后打印字符串
- 如何打印字符串而不是数字
- 使用 for 循环打印字符串数组无法正常工作 (C++)
- C++ 打印字符串的内容
- 如何在空格后打印字符串的其余部分
- 如何在C 中打印字符串向量的向量
- 当我打印字符串时,我不知道它是什么输出
- 尝试以随机字符(长读取)打印字符串返回结果
- 从左到右打印字符串的组合(不是排列)c++
- cout 不能一致地打印字符串和变量值,导致输出未对齐
- C++ cout 在擦除换行符后打印字符串不正确
- 从对象数组打印字符串对象
- C++ 粘贴值和打印字符串