1. 程式人生 > >C++讀取字串中的數字的方法

C++讀取字串中的數字的方法

程式碼例項:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
    string str("55.5818061829 119.6388702393 22.33");
    double t;
    istringstream iss;
    iss.str(str);
    while(iss>>t) {
        cout<<t<<endl;
    }
    return
0; }

注意,字串中除了數字之外,不要有任何多餘的符號,否則會失敗。在不熟悉正則表示式的情況下,這是一個很好的方式,尤其是讀取檔案中資料的時候。

下面給出讀取檔案的操作,檔案由數字組成,先給出檔案的資料

1.467284085736863 9.376511821522355 1.3649498766482537 8.088857549330717 2.3698882782758313 0.5225871352552025 0.6013677353526695 0.9871267670749978 0.7305225913374725 8.77 8.14 2.95 1.00 
6.041632454180849 4.535001565129676 3.3419336714886585 1.7315455144763214 9.220522172161063 9.793391925366667 0.2878894971039667 0.7862232406898116 0.72268766617564 5.56 15.43 8.38 1.00 
0.6158453075118819 7.724459956144301 0.5463212721843924 9.418369709345624 3.497235547312484 3.8047032003875527 0.734118480988751 0.4060951140458058 0.5793220401522091 8.59 7.85 8.11 1.00 
4.7685414389496525 3.5561636748781944 0.7380024353322878 6.738748696346324 9.993213301145895 6.607137410686869 0.2675133058358534 0.7928485178120843 0.6138402863139218 8.13 14.66 4.31 1.00 
0.07521491667869085 3.3382590774260956 7.8307214874066124 2.4160863939202546 5.347946916067755 1.3302075851573736 0.08288941437376163 0.3486248641364451 0.43751931623649365 3.73 7.53 8.12 1.00 
7.188894531065696 9.63371912799707 0.3937955195872622 3.4064493716649604 0.7316971537091621 2.3915790814548954 0.6686869130889267 0.380859571965775 0.5119530902547114 7.86 10.32 4.97 1.00 
3.2765247206521178 7.961855941273122 9.64131927218312 8.043113936024561 5.594476179791733 7.052954362787583 0.02976772757227908 0.9069645286116979 0.2781294472977186 15.02 12.89 10.36 1.00 
8.9009368950318 4.304817904602576 0.5774683034004457 5.1252895259247815 0.5573906664234685 3.8393256771269457 0.01789943667291516 0.34684869384120254 0.21521074875512458 11.66 6.17 1.19 1.00 
3.4688770006932534 6.7391094786095405 2.968489720797179 4.404010211807334 7.347207015679417 7.328173698226389 0.8430421343154131 0.29390949142436906 0.8157082348902395 6.28 11.24 12.02 1.00 
1.7647372666533434 7.95513095522135 0.4604028337109789 3.3475624805081674 9.57207772646172 7.98047012813255 0.3955382216600083 0.1154991722055756 0.6186918869242125 1.36 15.41 10.54 1.00 

資料全部由空格分隔開,每行的資料個數是相同的,下面給出讀取並輸出的示例程式碼:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main() {
    // 每個詞讀取,按照空格進行分隔
    ifstream fin("transform.txt");
    string s;
    while(fin>>s) {
        cout<<s<<endl;
    }
    fin.
close(); s.clear(); cout<<"==============================="<<endl; // 按行讀取,每行的結束是回車區分 fin.open("transform.txt"); while(getline(fin,s)) { cout<<s<<endl; } return 0; }

結合之前讀取資料的程式碼,一般可以這樣組合讀取測試資料:

#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;

int main() {
    ifstream fin;
    istringstream iss;
    string s;
    double t;
    // 按行讀取,每行的結束是回車區分
    fin.open("transform.txt");
    while(getline(fin, s)) {
        iss.clear();
        iss.str(s);
        while(iss>>t) {
            cout<<t<<" ";
        }
        cout<<endl;
    }
    return 0;
}

這種方式特別適合讀取測試資料,尤其是單元測試的時候。