stringstream或istringstream的使用

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <sstream>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main(){
    // 使用 stringstream
    string sinput;
    while (getline(cin,sinput)){
        stringstream ss;
        ss << sinput;
        long long tmp,ans=0;
        while (ss >> tmp){
            ans += tmp;
        }
       cout << ans << endl;
    }
    return 0;
}







#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <sstream>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main(){
    // 使用 istringstream
    string sinput;
    while (getline(cin,sinput)){
        istringstream ss(sinput);
        long long tmp,ans=0;
        while (ss >> tmp){
            ans += tmp;
        }
       cout << ans << endl;
    }
    return 0;
}




全部评论

相关推荐

球Offer上岸👑:可能是大环境太差了 太卷了 学历也很重要 hc也不是很多 所以很难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务