用map实现数字分离

#pragma warning(disable:4786)
#include
#include
#include
using namespace std;


int main()
{
map 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;

for (i=0; i {
sum+=m[sa[i]];
}

cout << "sum =" << sum << endl;

return 0;

}

你可能感兴趣的:(STL容器的基本操作)