c++中 string 和 int 类型转换(转)

一、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 << '\n'
                  << "to_string: " << f_str2 << "\n\n"
                  << "std::cout: " << f3 << '\n'
                  << "to_string: " << f_str3 << "\n\n"
                  << "std::cout: " << f4 << '\n'
                  << "to_string: " << f_str4 << "\n\n"
                  << "std::cout: " << f5 << '\n'
                  << "to_string: " << f_str5 << '\n';
    }

 输出:

    std::cout: 23.43
    to_string: 23.430000
     
    std::cout: 1e-09
    to_string: 0.000000
     
    std::cout: 1e+40
    to_string: 10000000000000000303786028427003666890752.000000
     
    std::cout: 1e-40
    to_string: 0.000000
     
    std::cout: 1.23457e+08
    to_string: 123456789.000000

补充:

to_wstring

(C++11)
    转换整数或浮点值为 wstring
(函数)

stoulstoull

(C++11)(C++11)
    转换字符串为无符号整数
(函数)

stoistolstoll

(C++11)(C++11)(C++11)
    转换字符串为有符号整数
(函数)

stofstodstold

(C++11)(C++11)(C++11)
    转换字符串为浮点值
(函数)

to_chars

(C++17)
    转换整数或浮点值到字符序列
(函数)

二、 string/char 转换为 int 类型

在C++编程的时候,有时需要将string类型转为int类型,则需要进行2部分:

1、首先将 字符串string 转为 C语言中的 const char* 类型(使用 _c.str()函数)

2、将 const char* 转为 int 类型(使用atoi(const char *)函数)

    注意:.c_str()函数在#include<string>的头文件中

               atoi()函数在#include<stdlib.h>的头文件中,如果使用,则必须包含该头文件

               同样的使用atox()函数(x代表要转换的类型)可以将const char* 转换为其它类型

1. 示例:

    #include <iostream>
    #include <string>
    #include <cstdlib>
     
    using namespace std;
     
    // 将string类型转为int类型,并于int类型进行判断
     
    int main(){
        string nums= "123";
        int num = 123;
        
        // 比较num与nums是否相等
        // 错误,因为int与string不能直接比较
    //    if(num == nums){
    //        cout << "num == nums" << endl;
    //    } else{
    //        cout << "num 与 nums 不相等@" << endl;
    //    }
        
        // 1、首先需要将nums转为const char*类型(使用.c_str()函数)
        // 2、在用atoi()函数将 const char *转为int类型
        if( num == atoi( nums.c_str() ) ) {
            cout << "num == nums" << endl;
        } else{
            cout << "num 与 nums 不相等@" << endl;
        }
    }

运行结果:

1)、不进行类型转换的结果

2)、进行转换后的结果:

 

     总结:

    通过使用.c_str()函数可以将string类型转换为const char * 类型,使用atoi()函数可以将const char*转换为int类型。

    其中atoi中的i表示int类型,如果转换为long类型则使用atol()函数。

2. 扩展补充

    在DEV-C++ (gcc,这里是C++98,没有支持C++11的特性)中定义的atoi函数:

        

    在Visio Stdio2019(不是GCC)中atoi()和.c_str()函数的定义为:

————————————————
版权声明:本文为CSDN博主「凉冰难消一腔热血」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45676049/article/details/108653320

posted on 2022-05-06 17:17  混元真人  阅读(1016)  评论(0)    收藏  举报