c++ io

这篇博客展示了两个C++程序,第一个程序使用getline和istringstream从标准输入读取每一行,并移除逗号,将结果存储并排序。第二个程序则计算连续输入的整数之和,每行结束后输出当前和。这两个程序分别处理了字符串处理和数值计算的基本操作。
摘要由CSDN通过智能技术生成

getline 去除每行输入字符串中的逗号

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

int main(){
    string s, tmp;
    vector<string> str;
    while(getline(cin, s)){
        istringstream is(s);
        while(getline(is, tmp, ','))
            str.push_back(tmp);
        sort(str.begin(), str.end());
        int i;
        for(i=0; i<str.size()-1; i++)
            cout<<str[i]<<",";
        cout<<str[i]<<endl;
        str.clear();
       
    }
      
    return 0;
}

求每行输入的数字之和,每行输入的数量不定

#include <iostream>

using namespace std;

int main(){
    int a, b, n, res=0;
    while(cin>>n){
        res += n;
        if(cin.get() == '\n'){
            cout<<res<<endl;
            res=0;
        }
    }
      
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值