C++ 输入接口

C++ 输入接口,第1张

求输入每一行的和
输入
1 2 3
4 5
0 0 0 0 0

输出
6
9
0

#include 
#include 
#include 
using namespace std;

int main() {
    string str;
    while (getline(cin, str)) {
        stringstream ss(str); // 将字符串 str 拷贝至 stringstream 流
        int sum = 0;
        int num = 0;
        while (ss >> num) sum += num; // 等效于 while (getline(ss, t, ' ')) sum += stoi(t);
        cout << sum << endl;
    }
    return 0;
}

用逗号分割每一行的字符串,排序后输出

输入
a,c,bb
f,dddd
nowcoder
输出
a,bb,c
dddd,f
nowcoder

#include 
#include 
#include 
using namespace std;

int main(){
    string line;
    while(getline(cin, line)){
        
        stringstream ss;
        ss << line;
        string str;
        
        vector ans;
        while(getline(ss, str, ','))  ans.push_back(str);
        sort(ans.begin(), ans.end());
        for(int i = 0; i

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/2889730.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-14
下一篇 2022-09-14

发表评论

登录后才能评论

评论列表(0条)