在从sstream变量中提取字符串后,如何获取剩余的字符串? [英] How do you get the remaining string after a string extracted from a sstream variable?

查看:639
本文介绍了在从sstream变量中提取字符串后,如何获取剩余的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像我有一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆