通常,当我用 C++ 编写任何内容时,我需要将char
进入一个int
我只是做了一个新的int
等于字符。
我使用了代码(片段)
string word;
openfile >> word;
double lol=word;
我收到的错误是
Code1.cpp cannot convert `std::string' to `double' in initialization
该错误到底意味着什么?第一个词是数字 50。谢谢:)
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << stod(" 99.999 ") << endl;
}
Output: 99.999
(这是双精度的,空格被自动去除)
由于 C++11 可通过函数将字符串转换为浮点值(如 double):
stof- 将 str 转换为浮点数
stod- 将 str 转换为 double
stold- 将 str 转换为 long double
由于问题中还提到了 string 到 int 的转换,因此 C++11 中有以下函数:
stoi- 将 str 转换为 int
stol- 将 str 转换为 long
stoul- 将 str 转换为 unsigned long
stoll- 将 str 转换为 long long
stoull- 将 str 转换为 unsigned long long
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)