C++stringstream按空格分割字符串时二次输入无效的解决方案

最近数据结构大作业有一个要做一个哈夫曼编码系统,里面涉及到树存入文件并恢复的问题。涉及到把字符串按照空格分割的问题,看到一个简便的方法就是通过stringstream流来处理。

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main() {
    //用于存放分割后的字符串 
    vector<string> res;
    //待分割的字符串,含有很多空格 
    string word = "   Hello, I want   to learn C++!   ";
    //暂存从word中读取的字符串 
    string result;
    //将字符串读到input中 
    stringstream input;
    input << word;
    //依次输出到result中,并存入res中 
    while (input >> result)
        res.push_back(result);
    //输出res 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    return 0;
}

输出结果如下图
在这里插入图片描述

这里我借鉴了这篇博客里的方法:C++中将string按照空白字符分割的新方法
另外,其实这里我是进行了改动的,我发现在用stringstream input(word)对其初始化时后面再用input<<word输入是无效的,即使你用了本文后面提到的方案,这里不改动,也是不行的。那篇博客中的代码是只适用于对单个字符串进行按空格分割的。嗯,看起来似乎没什么问题,但是在我的代码中,需要对多行进行处理,而不仅仅是处理这一行字符串的时候,问题就出现了。

#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main() {
    //用于存放分割后的字符串 
    vector<string> res;
    //待分割的字符串,含有很多空格 
    string word = "   Hello, I want   to learn C++!   ";
    //暂存从word中读取的字符串 
    string result;
    //将字符串读到input中 
    stringstream input;
    input << word;
    //依次输出到result中,并存入res中 
    while (input >> result)
        res.push_back(result);
    //输出res 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }

    cout << "n下面是第二次输入时再输出n";
    input << "this is a new string";
    while (input >> result)
        res.push_back(result);
    //输出res 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    return 0;
}

这里的代码,按照上面的逻辑,我应该除了Hello到C++!这几个string外,应该还有this is…到string这几个对象,但是下面的输出结果却不是我们想象的那样:
在这里插入图片描述
也就是说第二次输入的那个字符串根本没有传到stringstream对象中,这就是问题所在。
这是为什么呢?
原因:
input在转换完word的时候,已经读取完缓冲区,因此input的状态变为ios::eofbit,是不会再继续接受输入了;所以后面再给input输入,他也接收不到。

解决方案:
需要在每次转换完一次后,重新把stringstream对象的input的状态重新设为ios::goodbit即可。

input.clear(ios::goodbit);
#include<iostream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main() {
    //用于存放分割后的字符串 
    vector<string> res;
    //待分割的字符串,含有很多空格 
    string word = "   Hello, I want   to learn C++!   ";
    //暂存从word中读取的字符串 
    string result;
    //将字符串读到input中 
    stringstream input;
    input << word;
    //依次输出到result中,并存入res中 
    while (input >> result)
        res.push_back(result);
    //输出res 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    //重置状态
    input.clear(ios::goodbit);
    cout << "n下面是重置了input的状态后第二次输入时再输出n";
    string tmp = "this is a new string";
    input << tmp ;
    while (input >> result)
        res.push_back(result);
    //输出res 
    for (int i = 0; i < res.size(); i++) {
        cout << res[i] << endl;
    }
    return 0;
}

输出:
在这里插入图片描述
可以看到这里就没有问题了。

原文链接: https://www.cnblogs.com/gao-hongxiang/p/12342418.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++stringstream按空格分割字符串时二次输入无效的解决方案

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/191428

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午5:43
下一篇 2023年2月12日 下午5:43

相关推荐