C++读取字符串中的数字的方法

版权声明:所有的博客都是个人笔记,交流可以留言或者加QQ:2630492017 https://blog.csdn.net/qq_35976351/article/details/84799484

代码实例:

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

这种方式特别适合读取测试数据,尤其是单元测试的时候。

猜你喜欢

转载自blog.csdn.net/qq_35976351/article/details/84799484