zoukankan      html  css  js  c++  java
  • C++ string 转 int

    本文转自:https://blog.csdn.net/weixin_42442319/article/details/115523072

     

    1. 内置的转换函数

     

    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        int i_a = 12345, i_b = -23;
        /* 正负数也都不受影响 */
        int a;
        string s_a, s_b;
        s_a = to_string(i_a);
        cout << s_a << " " << sizeof(s_a) << endl;      //12345 32
        /* 为什么是32个字节? 因为sting是一种容器,它在内存中就先开辟了32个字节 */
        s_b = to_string(i_b);
        cout << s_b << " " << sizeof(s_b) << endl;      //-23 32
    
        a = stoi(s_a);
        cout << a << " " << sizeof(a) << endl;          //12345 4
        i_b = stoi(s_b);
        cout << i_b << " " << sizeof(i_b) << endl;      //-23 4 
    }
    

      

    2. 自己封装转换函数

    2.1 string转int

      • 最基本的转换函数应该怎么写
      • 还需要考虑的因素
        1)传入的字符串为空
        2)符号位的处理
        3)错误字符的处理
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Solution {
    public:
        int str_to_int(const string str) {
            /* 传入的字符串为空 */
            if(str.compare("") == 0 || str.length() == 0) {
            /* 这两项都是可以判断字符串为空的标志 */
                cout << "empty string" << endl;
                return 0;
            }
    
            /* 符号位的处理 */
            bool negative = true;
            int pos = 0;
            if(str[0] == '-') {
                negative = false;
                pos++;
            } else if(str[0] == '+'){
                pos++;
            }
            /* 最基本的写法 */
            int number = 0;
            while (pos < str.length())
            {
                /* 错误字符的处理 */
                if(str[pos] <= '9' && str[pos] >= '0') {
                    number *= 10;
                    number += (str.at(pos) - '0');
                    pos++;
                } else {
                    cout << "invaild string" << endl;
                    return 0;
                }
                
            }
            
            return negative ? number : -number;
        }
        // string int_to_str(const int number) {
    
        // }
    };
    int main() {
        Solution s;
        string str1 = "456";
        int num;
        num = s.str_to_int(str1);
        cout << num << endl;        //456
        s.str_to_int("");           //empty string
    
        string str2 = "+56", str3 = "-88";
        num = s.str_to_int(str2);
        cout << num << endl;        //56 
        num = s.str_to_int(str3);
        cout << num << endl;        //-88 
    
        string str4 = "adf12";
        s.str_to_int(str4);         //invaild string
    
    }
    

    2.2 int转string

      (待补)

    3. 数据范围

      在string转int的过程中,还可能出现转换为int却超出了int的存储范围。针对数据范围的问题,由一个问题引出。

      3.1 问题描述

        给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。
        如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。

       

      3.2 问题分析

        3.2.1 为什么是 [−231, 231 − 1]

        因为int类型是4个字节,最前面的一位是符号位。

        3.2.2 什么方法可以将数字反转

        %10可以将一个数字从低位开始取,而/(10*(位数-1))可以将一个数字从最高位取。

        3.2.3 新转换后的数字用什么类型存储

        231 − 1 = 2147483647,将这个数字反转后7463847412,明显int装不下,要用long。

        3.2.4 负数取余

        两个异号的数取余之后的结果取决于分子的符号。

        

      3.3 实现

        

    #include <iostream>
    using namespace std;
    class Solution {
    public:
        int reverse(int x) {
            bool negative = true;
            if(x < 0) {
                negative = false;
                x = abs(x);
                /* 将负数转为正数 */
            }
            long n = 0;
            // cout << sizeof(long) << endl;   //8
            while(x) {
                n = n*10 + x%10;
                x /= 10;
            }
        
            return (int)n == n ? negative ? n : -n : 0;
            /* (int)n == n这个语句判断了n有没有超过int的范围,太秒了,打call*/
        }
    };
    int main() {
        int a = 10, b = 0, c = 120, d = -123;
        int result;
        Solution s;
        result = s.reverse(a);
        cout << result << endl;
        result = s.reverse(b);
        cout << result << endl;
        result = s.reverse(c);
        cout << result << endl;
        result = s.reverse(d);
        cout << result << endl;
    }
    

      

    4. 总结

    • 不能仅仅注意代码的功能,比如参数的正确性、参数的范围以及异常情况的处理等等。
    • https://blog.csdn.net/qq_43152052/article/details/101023628
    • https://blog.csdn.net/xiao_nian/article/details/82251999
  • 相关阅读:
    binary and out mode to open a file
    ADV7482&TP2825开发之总结
    C++ 操作符重载
    OpenCV学习(一)基础篇
    Linux设备驱动程序 第三版 读书笔记(一)
    My First Linux Module
    Bitmap每个像素值由指定的掩码决定
    C++ File Binary
    Bitmap RGB24 4字节对齐
    查看binlog的简单方法!
  • 原文地址:https://www.cnblogs.com/liangzige/p/15590468.html
Copyright © 2011-2022 走看看