我必须编写一个程序,从字符串中获取数字,然后对这些数字求和。
示例:字符串 test="12,20,7"; 结果=50
有人能帮我吗?泰
string stringNumber="12,20,7";
vector<int> test;
vector<int> position;
string help;
int br=0;
int a;
for(int x=0; x<stringNumber.length(); x++)
{
if(stringNumber.at(x) !=';'){ //save numbers
help=stringNumber.at(x);
istringstream istr(help);
istr>>a;
test.push_back(a);
br++;
}
if(stringNumber.at(x) ==';'){ //save position of ","
position.push_back(br);
br++;
}
}