zoukankan      html  css  js  c++  java
  • 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 << "
    下面是第二次输入时再输出
    ";
        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 << "
    下面是重置了input的状态后第二次输入时再输出
    ";
        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;
    }
    

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

  • 相关阅读:
    排序算法整理
    V-REP Plugin 开发
    YAML-CPP
    YAML
    V-REP Remote API
    V-REP Plugin
    结构化方法与面向对象方法的比较
    敏捷软件开发vs传统软件工程
    个人项目-地铁出行路线规划程序
    Week1个人作业
  • 原文地址:https://www.cnblogs.com/gao-hongxiang/p/12342418.html
Copyright © 2011-2022 走看看