#include <stdio.h> #include <stdlib.h> #include <iostream> #include <string.h> #include <map> //#include <sys/time.h> using namespace std; void main() { //定义map对象,当前没有任何元素 map<char, int> m; //赋值:字符映射数字 m['0'] = 0; m['1'] = 1; m['2'] = 2; m['3'] = 3; m['4'] = 4; m['5'] = 5; m['6'] = 6; m['7'] = 7; m['8'] = 8; m['9'] = 9; /**//*上面的10条赋值语句可采用下面这个循环简化代码编写 for(int j = 0 ; j < 10 ; j++) { m['0' + j] = j ; } */ string sa, sb; sa = "6234"; int i; int sum = 0; int factor = 1; for (i = sa.length() - 1; i >= 0; i--) { if (i != sa.length() - 1) { factor *= 10; } sum += m[sa[i]] * factor; } cout << "sum = " << sum << endl; }