C++ 如何将变量字符串[]转换为浮点

C++ 如何将变量字符串[]转换为浮点,c++,visual-c++,visual-studio-2012,C++,Visual C++,Visual Studio 2012,如何将字符串转换为浮点 这是我的代码: string A[3] = {"20","21"}; float convertA ; convertA = atof (A[1]) ; cout << convertA << endl ; 字符串A[3]={“20”,“21”}; 浮动变流器; convertA=atof(A[1]); 无法阅读atof()的手册页,convertA应为double类型 double atof (const char* str); 及 应该是

如何将字符串转换为浮点

这是我的代码:

string A[3] = {"20","21"};
float convertA ;

convertA = atof (A[1]) ;
cout << convertA << endl ;
字符串A[3]={“20”,“21”};
浮动变流器;
convertA=atof(A[1]);

无法阅读
atof()
的手册页,
convertA
应为
double
类型

double atof (const char* str);

应该是

convertA = atof(A[1].c_str());
工作代码

#include<iostream>
#include<stdlib.h>
int main() {
        std::string A[3] = {"20","21"};
        double convertA ;

        //convertA = atof (A[1]) ;
        convertA = atof(A[1].c_str());
        std::cout << convertA << std::endl ;

        return 0;
}
#包括
#包括
int main(){
std::字符串A[3]={“20”,“21”};
双转换器;
//convertA=atof(A[1]);
convertA=atof(A[1].c_str());

std::cout它甚至可以编译吗?那些字符串是
std::string
s吗?这不会编译。您需要:
convertA=atof(A[1].c_str())
,除非
string
不是
std::string
;我不能只用4行代码就知道。请在
std::string
变量上使用
std::stof
atof
用于C字符串,即
const char*
输出仍然发出-1。#这不是您的完整代码。请发布一个。有。是的@super你是对的。输出仍然是-1IND@rafinsanovriardhira编译我回答的代码,它会工作的。数据=21是真的,但是有一个变量-1
#include<iostream>
#include<stdlib.h>
int main() {
        std::string A[3] = {"20","21"};
        double convertA ;

        //convertA = atof (A[1]) ;
        convertA = atof(A[1].c_str());
        std::cout << convertA << std::endl ;

        return 0;
}