【发布时间】:2018-02-26 03:23:53
【问题描述】:
我试图弄清楚如何将用户输入的string 与空格作为单个string。此外,在此之后,用户将包含其他strings,以逗号分隔。
例如,foo,Hello World,foofoo,其中foo 是一个string,后跟Hello World 和foofoo。
我现在拥有的是将Hello World 拆分为两个strings,而不是将它们合并为一个。
int main()
{
string stringOne, stringTwo, stringThree;
cout << "Enter a string with commas and a space";
cin >> stringOne; //User would enter in, for this example foo,Hello World,foofoo
istringstream str(stringOne);
getline(str, stringOne, ',');
getline(str, stringTwo, ',');
getline(str, stringThree);
cout << stringOne; //foo
cout << endl;
cout << stringTwo; //Hello World <---should be like this, but I am only getting Hello here
cout << endl;
cout << stringThree; //foofoo
cout << endl;
}
如何将Hello World 作为单个字符串而不是两个字符串转换为stringTwo。
【问题讨论】:
-
您有什么问题要问我们吗?
-
@RSahu 抱歉,我的问题是如何将
Hello World作为单个字符串而不是两个字符串转换为stringTwo。 -
逗号分隔的数据在How can I read and parse CSV files in C++?中处理
-
使用
std::getline()而不是operator>>来读取用户的输入:getline(cin, stringOne);