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;
}

輸出:
在這里插入圖片描述
可以看到這里就沒有問題了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM