麻烦:将字符串转换为双精度
trouble : convert string to double
当我将字符串转换为双精度变量时,我遇到了问题。
#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
相关文章:
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 在 c++ 中从双精度转换为字符串
- 使用 sprintf 和 %g 将双精度转换为字符串的意外结果
- 从双精度转换为整数的显式类型是否始终检查整数溢出?
- C++,遇到将双精度转换为整数的问题
- 在C++中将双精度转换为intptr_t
- 将大双精度转换为科学记数法以用作字符串?
- 了解双精度转换与整数转换中的整数与截断关系
- 将 OLE 自动化日期 (OADate) 双精度转换为结构 tm,而不使用 VariantTime到SystemTime
- 无法在赋值中将"双精度 (*)[4]"转换为"双精度**"
- 将大双精度转换为无符号 int 期间堆栈损坏
- 将双精度转换为本地8位
- 警告从"双精度"转换为"int"
- 如何将双精度转换为浮点指针
- 将双精度转换为整数时出错
- 使用并集将双精度转换为十六进制再转换为双精度
- 是否有可以将每个双精度转换为唯一uint64_t,保持精度和顺序的功能?(为什么我找不到?
- 使用定点表示法将双精度转换为字符串,没有尾随零和机智的冲刺
- 在C++中将双精度转换为字符*/字符串
- 无法将双精度 [] [] 转换为双精度 **