C++string类型转换成double
时间: 2023-09-25 08:14:10 浏览: 163
在C++中,可以使用stringstream来将string类型转换为double类型。可以使用以下代码实现:
```cpp
#include <iostream>
#include <sstream>
using namespace std;
double string_to_double(string tempString) {
double result;
stringstream ss;
ss << tempString;
ss >> result;
return result;
}
int main() {
string tempString;
cout << "输入:";
cin >> tempString;
double afterTrans = string_to_double(tempString);
cout << afterTrans << endl;
return 0;
}
```
这段代码中,我们定义了一个`string_to_double`函数,它接受一个string类型的参数,并使用stringstream将其转换为double类型。然后,在主函数中,我们输入一个string类型的值,调用`string_to_double`函数将其转换为double类型,并输出结果。
#### 引用[.reference_title]
- *1* [C++ string :数值型 string 字符串快速转换为 double 类型](https://blog.csdn.net/wangyuankl123/article/details/102727459)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [C++: string to double](https://blog.csdn.net/maxzcl/article/details/117413387)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文