如果字符串是从字符类型中给定的,则该字符串是否可以包含换行符
Can a string hold a newline character if given from a char type?
我的意思是。假设我们从标准输入中读取一堆文本,然后一个接一个地将其读取为字符类型。如果其中一个字符是换行符,那么这个变量显然可以保存关于换行符的信息。如果我将这个字符推送到字符串流中,然后将字符串流的内容输出到字符串中,该怎么办?
这个新字符串似乎没有包含任何关于换行符的数据。
有没有让字符串保存这些信息?
代码片段:
stringstream ssChar;
unsigned char aChar;
string strChar;
sourceFile >> noskipws >> aChar;
ssChar << aChar;
getline(ssChar, strChar);
//ssChar.str("");
//ssChar.seekg(0);
cout << "Next char is: " << (int)aChar << endl;
cout << "Length of char(from stringstream): " << strChar.length() << endl;
输入:带有换行符的文件xxd源文件
0000000:0a0a(实际上是2个换行符)。。
输出:下一个字符是:10(ascii换行符)字符长度:0(str为空)
答案是肯定的。当然,字符串会存储传递到其中的字符数据。代码中可能存在一些格式化操作,这些操作占用了空白或换行符。
为什么不测试它?
> cat temp.cc
#include<iostream>
using namespace std;
int main()
{
string str="vijayn";
cout << str <<"this is the next line"<<endl;
return 0;
}
> ./a.out
vijay
this is the next line
>
相关文章:
- C++-字符串是否包含一个带有简单循环的单词
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 检查字符串是否"null" C++
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 检查字符串是否为整数
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 如何检查字符串是否正确以将其转换为 int?
- 程序检查字符串是否只包含 1 和 0?
- 检查字符串是否是拉平德罗姆
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- gtest:检查字符串是否等于两个字符串之一
- 如何检查字符串是否包含所有这些:数字、字母和特殊字符
- 如何检查字符串是否以C++中的"ed"结尾;
- 字符串是否在其构造函数中复制数据
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何检查一个字符串是否包含多个其他字符串?
- 检查字符串是否仅包含字母
- 如何根据一个特定字母的出现来确定两个字符串是否相等?
- 查找字符串是否包含字符串向量的任何一个元素的最佳方法