一、int 类型转换为 string 类型

示例:
#include <iostream>
#include <string>
int main()
{
double f = 23.43;
double f2 = 1e-9;
double f3 = 1e40;
double f4 = 1e-40;
double f5 = 123456789;
std::string f_str = std::to_string(f);
std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000"
std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40".
std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000"
std::string f_str5 = std::to_string(f5);
std::cout << "std::cout: " << f << '\n'
<< "to_string: " << f_str << "\n\n"
<< "std::cout: " << f2 << '

本文详细介绍了在C++中如何进行string到int以及int到string的类型转换。主要内容包括使用.c_str()函数将int转换为const char*,然后通过atoi()函数将const char*转换为int。同时,文章提到了转换过程中的注意事项,如包含相应的头文件,并指出不同的编译器可能有不同的实现方式。
最低0.47元/天 解锁文章
449

被折叠的 条评论
为什么被折叠?



