在从sstream变量中提取字符串后,如何获取剩余的字符串? [英] How do you get the remaining string after a string extracted from a sstream variable?
问题描述
像我有一个 stringstream
变量包含abc gg rrr ff
当我对 stringstream
变量使用>>时,它会给我abc
。如何获取剩余的字符串:gg rrr ff
?似乎 str()
也不是 rdbuf()
这样工作。
std :: getline
从流中获取其余的字符串: #include< iostream>
#include< sstream>
using namespace std;
int main(){
stringstream ss(abc gg rrr ff);
string s1,s2;
ss>> s1;
getline(ss,s2); //获取字符串的剩余部分!
cout<< s1<< endl;
cout<< s2 < endl;
return 0;
}
输出:
abc
gg rrr ff
演示: a href =http://www.ideone.com/R4kfV> http://www.ideone.com/R4kfV
有一个重载 std :: getline
函数,其中第三个参数接受分隔符,您可以读取该字符串。请参阅 std :: getline
的文档:
Like I have a stringstream
variable contains "abc gg rrr ff"
When I use >> on that stringstream
variable, it gives me "abc"
. How can I get the remaining string: " gg rrr ff"
? It seems neither str()
nor rdbuf()
does this work.
You can use std::getline
to get the rest of the string from the stream:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss("abc gg rrr ff");
string s1, s2;
ss >> s1;
getline(ss, s2); //get rest of the string!
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
Output:
abc
gg rrr ff
Demo : http://www.ideone.com/R4kfV
There is an overloaded std::getline
function in which a third parameter takes a delimiter upto which you can read the string. See the documentation of std::getline
:
这篇关于在从sstream变量中提取字符串后,如何获取剩余的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!