溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++ stringstream格式化輸出輸入分析

發(fā)布時(shí)間:2021-11-12 13:47:47 來源:億速云 閱讀:302 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“C++ stringstream格式化輸出輸入分析”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C++ stringstream格式化輸出輸入分析”吧!

目錄

    最近在筆試時(shí)經(jīng)常遇見各種輸入問題,于是細(xì)心總結(jié)一波;首先string str; cin>>str;遇到空格結(jié)束;于是乎產(chǎn)生了getline(),可與得到一行字符串;空格自動去掉,只要不講cin和getline混用即可

     cin.getline(s,k);

        接收一行中k個字符,可以接收空格
        cin.getline()實(shí)際有三個參數(shù),cin.getline(字符串,接收個數(shù),結(jié)束字符);
        當(dāng)?shù)谌齻€參數(shù)省略時(shí),系統(tǒng)默認(rèn)為 '\0' ;

     getline(cin,s);

        和cin.getline()類似,讀入一行字符串,值得注意的是cin.getline()屬于istream流,而getline()屬于string流,二者并不相同。

    StringStream

    這個東西單獨(dú)講,比較重要,包含在sstream庫中。

    • istringstream類用于執(zhí)行C++風(fēng)格的串流的輸入操作。

    • ostringstream類用于執(zhí)行C風(fēng)格的串流的輸出操作。

    • stringstream類同時(shí)可以支持C風(fēng)格的串流的輸入輸出操作。

    然后stringstream的作用就是從string對象讀取字符或字符串。

     

    string s = "ABCD";
        stringstream ss(s);
        char ch;
        while(ss>>ch){
            cout << ch << " ";
        }
    
        //運(yùn)行結(jié)果
        //A B C D

    又例如:

    string s = "hello world";
        stringstream ss(s);
        string str;
        while(ss>>str){
            cout << str << " ";
        }
    
        //運(yùn)行結(jié)果
        //hello world

    在某些題目需要處理字符串時(shí),這些題目往往是輸入的一行中包含多個字符以及空格,這個時(shí)候就可以利用 stringstream進(jìn)行單個字符或者單個字符串分析處理了

      例子程序:

    int main()
    {
    
        string line;
        int k = 1;
        cout << "===============case1================" << endl;;
        while (getline(cin, line)) //可與讀到包含空格, ;等字符;但是在ss>>x時(shí)被截?cái)?
        {
            int sum = 0, x;
            stringstream ss(line);
            while (ss >> x)
            {
                sum += x;
            }
            cout << "the sum is :" << sum << endl;
            ++k;
            cout << endl;
            cout << "===============case" << k << "================" << endl;;
        }
    
        return 0;
    }

    -輸出:

    ===============case1================
    1 2 3 4 5
    the sum is :15

    ===============case2================
    1,2,3,4,5
    the sum is :1

    ===============case3================
    1 a 2 b 3
    the sum is :1

    ===============case4================
    a 1 1 1 1
    the sum is :0

    ===============case5================

    另外一組:

    int main()
    {
    
        string line;
        int k = 1;
    
        cout << "===============case1================" << endl;;
        while (getline(cin, line))
        {
            string out, x;
            stringstream ss(line);
            while (ss >> x)
            {
                cout << x << ";";
            }
            ++k;
            cout << endl;
            cout << "===============case" << k << "================" << endl;;
        }
    
        return 0;
    }

     輸出:

    ===============case1================
    this is very good!
    this;is;very;good!;
    ===============case2================
    this,is,very,good!
    this,is,very,good!;
    ===============case3================

    實(shí)驗(yàn)矩陣類型的輸入:

    3

    0 1 2

    2 3 4

    5 6 7

    int main()
    {
    
        string line;
        int k = 1;
     //測試矩陣形式的輸入:
        string input;
        int n;
        //cin >> n; //輸入n行數(shù)據(jù),如果后面用getline()后面的換行符不能處理
        getline(cin, input);
        stringstream ss(input);
        ss >> n;
        
        vector<vector<int> > vec;
        for (int i = 0; i < n;i++)
        {
            getline(cin,input); //會將換行符當(dāng)做一行
            stringstream ss(input);
            int x;
    
            vector<int> temp;
            while (ss>>x) //只能以空格處理分離
            {
                temp.push_back(x);
            }
            vec.push_back(temp);
        }
    
    
        return 0;
    }

     使用方法:

    • input1是沒有空格的,帶有,;將矩陣的行列分出來;

    • 但是使用cin>>k后,沒有使用getline(input1), 應(yīng)為input1本身是沒有空格的字符串

    C++ stringstream格式化輸出輸入分析

    經(jīng)上面啟發(fā):練習(xí)這樣的輸入:

    3 3
    0,1 0,2;0,0 1,0;0,1 1,1;0,2 1,2;1,0 1,1;1,1 1,2;1,1 2,1;1,2 2,2;2,0 2,1

    測試代碼:

    int main()
    {
    
        string line;
        int k = 1;
    
        int row, col;
        getline(cin, line);
        stringstream ss(line);
        ss >> row >> col;
        getline(cin, line); //第二行
        stringstream s(line);
        string temp1, temp2, temp3;;
        vector<pair<pair<int,int>,pair<int,int>>> vec;
        while (getline(s, temp1, ';'))
        {
            vector<pair<int, int>> pair1;
            stringstream s3(temp1);
            while (getline(s3, temp2,' ')) //以換行符結(jié)束,中間為空格
            {
                pair<int, int> pair2;
                stringstream s4(temp2);
                vector<int> res;
                while (getline(s4, temp3, ','))
                {
                    res.push_back(stoi(temp3));
                }
                pair2.first = res[0];
                pair2.second = res[1];
    
                pair1.push_back(pair2);
            }
    
            vec.push_back(make_pair(pair1[0],pair1[1]));
        }
    
    
        return 0;
    }

    輸出:

    C++ stringstream格式化輸出輸入分析

    另外參考輸入遇到過問題題解:

    5 0
    1 2 3
    0 4
    0 4
    0 4
    1 2 3
    
    int main()
    {
        int N, id;
        string str;
        getline(cin, str);
        stringstream ss(str);
        ss >> N >> id;
    
        vector<vector<int>> vec;
        //for (int i = 0; i < N; i++)
        //{
        //  vector<int> temp;
        //  int user;
        //  getline(cin, str);
        //  stringstream s(str);
        //  while (s>>user) //以空格進(jìn)行劃分
        //  {
        //      temp.push_back(user);
        //  }
        //  vec.push_back(temp);
        //}
    
        while (getline(cin, str)) //其實(shí)可都可以不知道行數(shù)
        {
            vector<int> temp;
            int user;
            stringstream s(str);
            while (s >> user)
            {
                temp.push_back(user);
            }
            vec.push_back(temp);
        }
    
        cout << recommendFriends(vec, id) << endl;
    
        return 0;
    }
    
    int test()
    {
        //int N,id;
        //cin >> N >> id; //直接輸入用戶數(shù)和需要查找的用戶id ; 這樣就會產(chǎn)生換行符
    
        int N;
        vector<int> in;
        char c;
        while ((c = cin.get()) != '\n')
        {
            cin.unget();
            cin >> N; 
            in.push_back(N);
        }
        
        vector<vector<int>> vec;
        for (int i = 0; i < in[0]; i++)
        {
            vector<int> temp;
            int user;
            
            while ((c=cin.get())!= '\n') //文件結(jié)果沒有換行符了,所以陷入死循環(huán)
            {
                cin.unget();
                cin >> user;
                temp.push_back(user);
            }
            if (temp.size()!=0)
            {
                vec.push_back(temp);
            }
            
        }
    
        cout << recommendFriends(vec, in[1]) << endl;
    
        return 0;
    }

    全部代碼:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <unordered_map>
    #include <unordered_set>
    
    #include<sstream>
    #include<fstream>
    using namespace std;
    
    #define cin infile //一定不能再oj系統(tǒng)中,有錯,導(dǎo)致超時(shí)等?。?!
    //C++文件輸入
    ifstream infile("ini.txt", ifstream::in);
    
    
    //函數(shù)功能:將輸入字符串s,以字符串c(;)進(jìn)行拆分,拆分結(jié)果放在v中
    //函數(shù)參數(shù)說明:s為輸入字符串;c為拆分的字符串;v為拆分結(jié)果
    //函數(shù)返回值:正常返回0
    int split_string(const std::string& s, std::vector<std::string>& v, const std::string& c)
    {
        std::string::size_type pos1, pos2;
        pos2 = s.find(c);
        pos1 = 0;
        while (std::string::npos != pos2)
        {
            v.push_back(s.substr(pos1, pos2 - pos1));
    
            pos1 = pos2 + c.size();
            pos2 = s.find(c, pos1);
        }
        if (pos1 != s.length())
            v.push_back(s.substr(pos1));
        return 0;
    }
    
    int main()
    {
    
        string line;
        int k = 1;
        /*cout << "===============case1================" << endl;;
        while (getline(cin, line))
        {
            int sum = 0, x;
            stringstream ss(line);
            while (ss >> x)
            {
                sum += x;
            }
            cout << "the sum is :" << sum << endl;
            ++k;
            cout << endl;
            cout << "===============case" << k << "================" << endl;;
        }*/
    
        //cout << "===============case1================" << endl;;
        //while (getline(cin, line))
        //{
        //    string out, x;
        //    stringstream ss(line);
        //    while (ss >> x)
        //    {
        //        cout << x << ";";
        //    }
        //    ++k;
        //    cout << endl;
        //    cout << "===============case" << k << "================" << endl;;
        //}
    
        ////測試矩陣形式的輸入:
        //string input;
        //int n;
        ////cin >> n; //輸入n行數(shù)據(jù),如果后面用getline()后面的換行符不能處理
        //getline(cin, input);
        //stringstream ss(input);
        //ss >> n;
        //
        //vector<vector<int> > vec;
        //for (int i = 0; i < n;i++)
        //{
        //    getline(cin,input); //會將換行符當(dāng)做一行
        //    stringstream ss(input);
        //    int x;
        //    vector<int> temp;
        //    while (ss>>x) //只能以空格處理分離
        //    {
        //        temp.push_back(x);
        //    }
        //    vec.push_back(temp);
        //}
    
        int row, col;
        getline(cin, line);
        stringstream ss(line);
        ss >> row >> col;
        getline(cin, line); //第二行
        stringstream s(line);
        string temp1, temp2, temp3;;
        vector<pair<pair<int,int>,pair<int,int>>> vec;
        while (getline(s, temp1, ';'))
        {
            vector<pair<int, int>> pair1;
            stringstream s3(temp1);
            while (getline(s3, temp2)) //以換行符結(jié)束,中間為空格
            {
                pair<int, int> pair2;
                stringstream s4(temp2);
                vector<int> res;
                while (getline(s4, temp3, ','))
                {
                    res.push_back(stoi(temp3));
                }
                pair2.first = res[0];
                pair2.second = res[1];
    
                pair1.push_back(pair2);
            }
    
            vec.push_back(make_pair(pair1[0],pair1[1]));
        }
    
    
        return 0;
    }

    感謝各位的閱讀,以上就是“C++ stringstream格式化輸出輸入分析”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C++ stringstream格式化輸出輸入分析這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI