2011-01-20 103 views
23

通常当我用C写东西++,我需要一个char转换成int我只是做一个新int等于字符。C++字符串双转换

我使用的代码(段)

string word; 
openfile >> word; 
double lol=word; 

我收到

Code1.cpp cannot convert `std::string' to `double' in initialization 

是什么错误恰恰意味着这个错误吗?第一个字是数50谢谢:)

+0

可能重复http://stackoverflow.com/questions/1710447/string-in-scientific-notation- c-to-double-conversion) – 2011-01-20 23:53:28

+0

这基本上是重复的,唯一不同的是你甚至不需要经过字符串阶段,因为你可以直接读入double。 – 2011-01-20 23:54:26

+3

将char转换为int时,您将得到该字符的代码,而不是它的感知“值”。换句话说,'int x ='0';`将`x`设置为`48`(如果您处于ASCII兼容区域),而不是`0`。 – 2011-01-20 23:56:28

回答

42

您可以将char转换为int,反之亦然容易,因为该机的INT和炭相同,8位,唯一的区别来当他们必须显示在屏幕上,如果数字是65并保存为字符,那么它将显示'A',如果它被保存为int将显示65.

随着其他类型的东西改变,因为它们在内存中的存储方式不同。在C中有标准函数,可以让你从字符串转换为double,很容易。 (您需要包括stdlib.h中)

#include <stdlib.h> 

int main() 
{ 
    string word; 
    openfile >> word; 
    double lol = atof(word.c_str()); /*c_str is needed to convert string to const char* 
            previously (the function requires it)*/ 
    return 0; 
} 
14

的问题是,C++是一种静态类型语言,这意味着如果事情被声明为string,这是一个字符串,如果事情被声明为double,这是双倍的。与JavaScript或PHP等其他语言不同,无法自动将字符串转换为数字值,因为转换可能没有明确定义。例如,如果您尝试将字符串"Hi there!"转换为double,则没有有意义的转换。当然,你可能只是将double设置为0.0或NaN,但这几乎肯定会掩盖代码中存在问题的事实。

要解决此问题,请不要将文件内容缓冲到字符串中。相反,只需直接读入double

double lol; 
openfile >> lol; 

这直接读取值作为实数,如果发生错误,将导致流的.fail()方法返回true。例如:

double lol; 
openfile >> lol; 

if (openfile.fail()) { 
    cout << "Couldn't read a double from the file." << endl; 
} 
4

如果你正在阅读文件,那么你应该听到给出的建议,并把它放到一个双。

另一方面,如果你确实有一个字符串,你可以使用boost的lexical_cast

这里是一个(很简单),例如:

int Foo(std::string anInt) 
{ 
    return lexical_cast<int>(anInt); 
} 
-1
#include <string> 
#include <cmath> 
double _string_to_double(std::string s,unsigned short radix){ 
double n = 0; 
for (unsigned short x = s.size(), y = 0;x>0;) 
if(!(s[--x]^'.')) // if is equal 
n/=pow(10,s.size()-1-x), y+= s.size()-x; 
else 
    n+=((s[x]-48) * pow(10,s.size()-1-x - y)); 
return n; 
} 

//In case you want to convert from different bases. 
#include <string> 
#include <iostream> 
#include <cmath> 
double _string_to_double(std::string s,unsigned short radix){ 
double n = 0; 
for (unsigned short x = s.size(), y = 0;x>0;) 
if(!(s[--x]^'.')) 
n/=pow(radix,s.size()-1-x), y+= s.size()-x; 
else 
    n+=((s[x]- (s[x]<='9' ? '0':'0'+7) ) * pow(radix,s.size()-1-x - y)); 
return n; 
} 
int main(){ 
std::cout<<_string_to_double("10.A",16)<<std::endl;//Prints 16.625 
std::cout<<_string_to_double("1001.1",2)<<std::endl;//Prints 9.5 
std::cout<<_string_to_double("123.4",10)<<std::endl;//Prints 123.4 
return 0; 
} 
0

丁文从字符串使用 '的strtod()' 从库函数来增加一倍 可以实现“STDLIB 。H”

#include <iostream> 
#include <stdlib.h> 
int main() 
{ 
    std::string data="20.9"; 
    double value = strtod(data.c_str(), NULL); 
    std::cout<<value<<'\n'; 
    return 0; 
} 
11
#include <iostream> 
#include <string> 
using namespace std; 

int main() 
{ 
    cout << stod(" 99.999 ") << endl; 
} 

输出:99.999(它是双,空格被自动剥离)

由于C++ 11转换字符串浮点值(例如双)是可用的功能:
stof - STR转换为float
stod - STR转换为双
stold - 转换STR为长双

作为字符串为int问题中还提到的转化率,有以下功能在C++ 11:
stoi - 转换STR为int
stol - 转换STR为长
stoul - 转换STR为无符号长
stoll - 转换STR为长长
stoull - 转换STR为无符号长长

0

的C++解决转换(未经典C)的方式示出与下面的程序。请注意,目的是能够使用iostream提供的相同格式设施,如精度,填充字符,填充,十六进制和操纵器等。

编译并运行该程序,然后研究它。这是简单

#include "iostream" 
#include "iomanip" 
#include "sstream" 
using namespace std; 

int main() 
{ 
    // Converting the content of a char array or a string to a double variable 
    double d; 
    string S; 
    S = "4.5"; 
    istringstream(S) >> d;  
    cout << "\nThe value of the double variable d is " << d << endl; 
    istringstream("9.87654") >> d; 
    cout << "\nNow the value of the double variable d is " << d << endl; 



    // Converting a double to string with formatting restrictions 
    double D=3.771234567; 

    ostringstream Q; 
    Q.fill('#'); 
    Q << "<<<" << setprecision(6) << setw(20) << D << ">>>"; 
    S = Q.str(); // formatted converted double is now in string 

    cout << "\nThe value of the string variable S is " << S << endl; 
    return 0; 
} 

Martinez的教授

[字符串科学记数法C++来双转换(的