类型转换 - 如何将C++ string 类型的每一位字符转换成int(并不是将整个string转换成int)?
PHPz
PHPz 2017-04-17 12:58:45
0
2
883

string str ="123456";
如何把str的每一位str[i]转换成数字?

已经尝试:
用int(str[i])只能得到ascii码。
用atoi(str.c_str())也只能将整个string转换成int。

PHPz
PHPz

学习是最好的投资!

reply all(2)
刘奇

You have already got the ASCII code, isn’t it just a number after subtracting ‘0’ (that is, 48)...

迷茫
#include <string>
#include <stdlib.h>

using namespace std;


int main(){
    string str = "123456";
    const char *p = str.c_str();
    for (int i = 0; i < str.length(); i++)
    {
        int a = str[i] - '0';
        printf("%d \n", a);

    }
    system("pause");
    return 0;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!