c++ string to float
时间: 2023-09-11 16:13:16 浏览: 184
test_string型转换成对应的float类型_float_读取txt文档_C++/C_vs2017_
To convert a C++ string to a float, you can use the `std::stof` function from the `<string>` library. Here's an example:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "3.14";
float floatValue = std::stof(str);
std::cout << "Float value: " << floatValue << std::endl;
return 0;
}
```
In this example, the `std::stof` function takes the string `str` and converts it to a float value. The resulting float value can then be used as needed.
阅读全文