麻烦:将字符串转换为双精度

trouble : convert string to double

本文关键字:双精度 转换 字符串 麻烦      更新时间:2023-10-16

当我将字符串转换为双精度变量时,我遇到了问题。

#include<iostream>
#include<fstream>
#include<string.h>
#include<sstream>
#include<vector>

using namespace std;
int main(){
    int cols,rows;
    cols=6;rows=15;
    string line;
    string **value;
    double *conv;
    string *rowsis;
    int i_line=0;
    int i,j,k,a,l;
    j=0;
    double ***datV;
    vector <string> v;
    vector <string> nVal;
    string delimiter="  ";
    size_t pos=0;
    string token;
    ifstream fin("data/profil.data");
    while(getline(fin,line)){
        v.push_back(line);
        i_line++;
    }
    value=new string*[i_line];
    rowsis=new string[i_line];
    for(i=0;i<i_line;i+=18){
        a=1+i;
        rowsis[j]=v[i];
        value[j]=new string[rows];
        for(k=0;k<rows;k++){
            value[j][k]=v[a+k];
            while((pos=value[j][k].find(delimiter))!=string::npos){
                token=value[j][k].substr(0,pos);
                value[j][k].erase(0,pos+delimiter.length());
                nVal.push_back(token);
            }
            nVal.push_back(value[j][k]);
        }
        j++;
    }
    int limit=(j*rows-1)+(j*rows*cols);
    datV=new double**[i_line];
    conv=new double[limit];
    int b=0;
    for(i=1;i<limit;i++){
        stringstream vd(nVal[i]);
        vd>>conv[b];
        //view result variable conv
        cout<<conv[b];
        b++;
    }
    int c=0;
    for(i=0;i<j;i++){
        datV[i]=new double*[rows];
        for(k=0;k<rows;k++){
            datV[i][k]=new double[cols];
            for(l=0;l<cols;l++){
                datV[i][k][l]=conv[c];
                //cout<<datV[i][k][l]<<" ";
                c++;
            }
            //cout<<endl;
        }
        //cout<<endl<<endl;
    }
    return 0;
}

和配置文件数据,如下所示:

normal
    -1  -1  63  -3  -1  0
    0   0   62  -3  -1  0
    -1  -1  61  -3  0   0
    -1  -1  63  -2  -1  0
    -1  -1  63  -3  -1  0
    -1  -1  63  -3  -1  0
    -1  -1  63  -3  0   0
    -1  -1  63  -3  -1  0
    -1  -1  63  -3  -1  0
    -1  -1  61  -3  0   0
    -1  -1  61  -3  0   0
    -1  -1  64  -3  -1  0
    -1  -1  64  -3  -1  0
    -1  -1  60  -3  0   0
    -1  0   64  -2  -1  0

normal
    -1  -1  63  -2  -1  0
    -1  -1  63  -3  -1  0
    -1  -1  61  -3  0   0
    0   -4  63  1   0   0
    0   -1  59  -2  0   -1
    -3  3   57  -8  -3  -1
    -1  3   70  -10 -2  -1
    0   -3  61  0   0   0
    0   -2  53  -1  -2  0
    0   -3  66  1   4   0
    -3  3   58  -10 -5  0
    -1  -1  66  -4  -2  0
    -1  -2  67  -3  -1  0
    0   1   66  -6  -3  -1
    -1  -1  59  -3  -4  0

nVal[i] 结果为:

-

1-163-3-100062-3-10-1-161-300

现在 conv[b] 是:

-1-163-3-10*0*

0062-3-10*0*-1-161-300*0*

nVal转换时添加了0个值,如何删除0个值?

请帮忙?

尝试使用 atof

#include <stdlib.h>
int main()
{
    string word;  
    openfile >> word;
    double lol = atof(word.c_str()); 
    return 0;
}

我必须尝试一些实验,最后我找到了解决方案:

for(i=1;i<limit;i++){
        stringstream vd(nVal[i]);
        if(!(vd>>conv[b])){
            b-=1;
        }
        vd>>conv[b];
        b++;
    }

谢谢大家,:D