字符串转化并累加
一行字符类型的数字,每个数字以空格隔开,将它们转化为数字后累加求和
1 #include <iostream> 2 #include <string> 3 #include <sstream> 4 using namespace std; 5 6 int main() { 7 string str; 8 while (getline(cin, str)) { 9 int sum = 0, num = 0; 10 stringstream ssio(str); 11 while (ssio >> num) 12 sum += num; 13 cout << sum << endl; 14 } 15 16 return 0; 17 }