C++中字符串和数字之间的转换

C++中字符和数字之间的转换


最近遇到看书和做题遇到的一些关于字符串和数字转化之间的问题,做一个整理。

sscanf 与 sprinf

具体可以参考《算法笔记》p53页

  1. sscanf的使用

    sscanf 是将字符数组的内容以对应数字形式(也可以是字符串的形式, 本篇主要讨论字符与数字的转化)传输到n中(从左到右),例如:

    #include <cstdio>
    int main(){
        int n;
        char c[100] = "8160";
        sscanf(c, "%d", &n);
        printf("%d", n / 2); // 4080
       /*
        char c[100] = "8160", t[20];
        sscanf(c, "%s", t);
        printf("%s", t); // 8160
       */ 
        return 0;
    }
    

    不同类型变量的组合:

    #include <cstdio>
    int main(){
        int n;
        double f;
        char c[100] = "abcde8160 13.1", t[20];
        sscanf(c, "%5s %d %lf", t, &n, &f);
        printf("%s %d %.1f", t, n / 2, f); // abcde 4080 13.1
        return 0;
    }
    

    出现符号时

    #include <cstdio>
    int main(){
        int n;
        double f;
        char *c = "abcde:8160,13.1", t[20];
        sscanf(c, "%5s:%d,%lf", t, &n, &f);
        printf("%s %d %.1f", t, n / 2, f); // abcde 4080 13.1
        return 0;
    }
    

    或者配合正则表达式

    #include <cstdio>
    int main(){
        int n;
        double f;
        char c[100] = "abcde8160asdf13.1", t[20];
       /* 写成指针类型也可以,本质一样
       	char *c = "abcde8160asdf13.1";
       */
        sscanf(c, "%5s %d %*[a-z]%lf", t, &n, &f);
        printf("%s %d %.1f", t, n / 2, f); // abcde 4080 13.1
        return 0;
    }
    

    使用对应的正则表达式,参考文章

    %*xxx   忽略匹配到的内容,后面的XXX是匹配的模式,即跳过这些
    %[xxx]  [ ]这是类正则表达式的基本形式,xxx里面就是它的匹配模式
    %[a-z]  匹配所有的小写字符,直到遇到不是小写字符
    %[0-9] 匹配所有的数字,直到遇到不是数字
    %[A-Z] 匹配所有的大写字符,直到遇到不是大写字符
    %[a-zA-Z] 匹配所有的字符,直到遇到不是字符
    %[^xxx]  匹配字符,直到xxx出现
    %[^a-z] 匹配所有的字符,直到遇到小写字符
    %[^0-9] 匹配所有字符,直到遇到数字
    %[^#] 匹配所有字符,直到遇到#
    
  2. sprintf功能正好相反,是将n以对应形式的的数字(也可以是字符串的形式,本篇主要讨论字符与数字的转化)写到字符数组中(从右到左),例如:

    #include <cstdio>
    int main(){
        int n = 8160;
        char c[50];
        sprintf(c, "%d", n);
        printf("%s", c); // 8160
       /*
        int n;
        char c[20], a[20] = "8160", b[20] = "4152";
        sprintf(c, "%s%s", a, b);
        printf("%s", c); // 81604152
       */
        return 0;
    }
    

    不同类型变量的组合

    #include <cstdio>
    int main(){
        int n = 8160;
        double f = 3.14159;
        char c[20], a[20] = "abcd";
        sprintf(c, "%d%.3f%s", n, f, a);
        printf("%s", c); // 8160  3.142  abcd
        /*
        如果中间存在 空格
         sprintf(c, "%d  %.3f  %s", n, f, a);
         输出为:8160  3.142  abcd   
         同理中间的位置就可以连接其他的字符 如abc
        */
        return 0;
    }
    

    实现中间位置的拼接

    #include <cstdio>
    int main(){
        int n = 8160;
        double f = 3.14159;
        char c[20], a[20] = "abcd";
        sprintf(c, "%dadc%.3f11%s", n, f, a);
        printf("%s", c); // 8160adc3.14211abcd
        return 0;
    }
    

    设定拼接字符宽度:

    #include <cstdio>
    int main(){
        int a = 8160, b = 123;
        char c[20];
        sprintf(c, "%6d%6d", a, b);
        printf("%s", c); //  8160   123
        return 0;
    }
    

    sprintf的用法很多,具体参考此篇,之后拥抱再继续补充。

stringstream的使用

  1. 字符串转化为数字

    #include <iostream>
    #include <sstream>
    using namespace std;
    int main(){
        stringstream ss;
        string s = "8160";
        int n;
        ss << s;
        ss >> n;
        cout << n;
        return 0;
    }
    
  2. 数字转为字符串

    #include <iostream>
    #include <sstream>
    using namespace std;
    int main(){
        stringstream ss;
        int a = 81060;
        string c;
        ss << a;
        ss >> c; // 或 c = ss.str();
        cout << c;
        return 0;
    }
    

stringstream还有其他的用法,日后总结。

调用stoi stol 等库函数

  1. string 与数字之间的转换, 一般的后两个参数可以 p,b是可选项,一般不需要的时候可以不写。 参考官方 文档

    s为要转化的字符串

    pos 为起始位置

    base对应的进制

    函数含义
    stoi(s, pos, base)需要转化为int类型的字符串s,p为起始位置,b为对应的进制
    stol(s, pos, base)需要转化为long类型的字符串s,p为起始位置,b为对应的进制
    stoll(s,pos, base)需要转化为long long类型的字符串s,p为起始位置,b为对应的进制
    stoul(s,pos,base)需要转化为unsigned long 类型的字符串s,p为起始位置,b为对应的进制
    stoull(s,pos,base)需要转化为unsigned long long 类型的字符串s,p为起始位置,b为对应的进制s
    stof(s,pos)需要转化为float 类型的字符串s,p为起始位置
    stod(s,pos)需要转化为double 类型的字符串s,p为起始位置
    stold(s,pos)需要转化为long double类型的字符串s,p为起始位置
    to_string(v)数字转化为字符转

    示例:

    #include <iostream>
    using namespace std;
    int main(){
        int n = 100;
        cout << to_string(n) << endl;
        string s = "16";
        cout <<  stoi(s) << endl;;
        return 0;
    }
    
  2. char 数组和数字之间的转化

    函数含义
    atoi©char类型的字符串转化为int类型值
    atof©char类型的字符串转化为float类型值
    atol©char类型的字符串转化为long类型值

    示例:

    #include <iostream>
    using namespace std;
    int main(){
        char c[20] = "8160";
        cout << atoi(c) / 2;
        return 0;
    }
    

其他的就日后补充啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值