关于字符串流的使用

版权声明:SupremeBeast3_ https://blog.csdn.net/weixin_43359312/article/details/89012504

关于字符串流sstream的使用

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
	string line;
	while (getline(cin, line)) {	//得到一行被空格隔开的待相加的数字
		int sum = 0, x;
		stringstream ss(line);	//创建字符串流
		while (ss >> x) sum += x;	//从ss中读取 读取到整数x时加和
		cout << sum << endl;
	}
	return 0;
}				//缺点是较为耗时 慎用

猜你喜欢

转载自blog.csdn.net/weixin_43359312/article/details/89012504