C++ 输入输出函数总结及 OJ 在线编程常见输入输出练习

C++ 输入

std::operator>>

#include <iostream>
#include <string>
istream& operator>> (istream& is, string& str);
  • 遇到空格(回车等)停止,但不会从输入中读取并丢弃空格;
  • 连续使用 cin 时,会自动跳过前导空格(回车等);

std::getline

#include <string>
istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&  is, string& str); // default delim '\n'
  • 遇到 delim 则停止读取,从输入缓冲读取并丢弃 delim
  • delim 不会写入 str,且下一个输入操作将从 delim 下一位开始;

std::istream::getline

#include <iostream>
istream& getline (char* s, streamsize n );  // default delim '\n'
istream& getline (char* s, streamsize n, char delim );
  • 遇到 delim 或已经读入 n-1 个有效字符则停止,函数会在 s 尾部自动添加终止空字符'\0'
  • 从输入缓冲读取 delim 并丢弃,不会写入字符串 s 中;

std::istream::get

#include <iostream>
int peek(); // no extracting
int get();  // extract
istream& get (char& c);
istream& get (char* s, streamsize n); // default delim '\n'
istream& get (char* s, streamsize n, char delim);
  • 读入 n-1 个有效字符 或 遇到 delim 则停止,且 delim 不会被读入并丢弃;
  • 空字符 '\0' 自动添加到 s 末尾;

std::istream::ignore

istream& ignore (streamsize n = 1, int delim = EOF);
  • 从输入序列中读取并丢弃字符,直到丢弃共计 n 个字符或遇到 delim;
  • delim 会被读取并丢弃

总结

  • 大部分函数都会返回输入流的引用 istream&,因此可以用于条件判断输入有效性,或连续输入;

牛客 OJ 输入输出练习

A+B(1)

输入描述:
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
输出描述:
输出a+b的结果

#include <iostream>
using namespace std;

int main(){
    int a, b;
    while(cin >> a >> b)
        cout << a + b << endl;
    return 0;
}

A+B(2)

输入描述:
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
输出描述:
输出a+b的结果

#include <iostream>
using namespace std;

int main(){
    int t;
    int a, b;
    cin >> t;
    while(t--){
        cin >> a >> b;
        cout << a + b << endl;
    }
    return 0;
}

A+B(3)

输入描述:
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出描述:
输出a+b的结果

#include <iostream>
using namespace std;

int main(){
    int a, b;
    while(cin >> a >> b){
        if(a == 0 && b == 0)
            break ;
        cout << a + b << endl;
    }
    return 0;
}

A+B(4)

输入描述:
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果

#include <iostream>
using namespace std;

int main(){
    int n, num;
    int sum = 0;
    while(cin >> n && n != 0){
        while(n--){
            cin >> num;
            sum += num;
        }
        cout << sum << endl;
        sum = 0;
    }
    return 0;
}

A+B(5)

输入描述:
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果

#include <iostream>
using namespace std;

int main(){
    int lines, n, num;
    int sum;
    cin >> lines;
    while (cin >> n) {
        sum = 0;
        while (n--) {
            cin >> num;
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

A+B(6)

输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果

#include <iostream>
using namespace std;

int main(){
    int n, num, sum;
    while (cin >> n) {
        sum = 0;
        while (n--) {
            cin >> num;
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

A+B(7)

输入描述:
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
输出描述:
每组数据输出求和的结果

// F1
#include <iostream>
using namespace std;

int main() {
    int sum, num;
    while (cin >> num) {
        sum += num;
        if (cin.get() == '\n') { // 提取空格或行尾回车
            cout << sum << endl;
            sum = 0;
        }
    }
    return 0;
}

// F2
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    int sum, num;
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        while (iss >> num)
            sum += num;
        cout << sum << endl;
        sum = 0;
    }
    return 0;
}

字符串排序(1)

输入描述:
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<string> vec(n);
    while(n--) cin >> vec[n];
    sort(vec.begin(), vec.end());
    for (string& str : vec)
        cout << str << " ";
    return 0;
}

字符串排序(2)

输入描述:
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
输出描述:
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main(){
    vector<string> vec;
    string tmp;
    while (cin >> tmp) {
        vec.push_back(tmp);
        if (cin.get() == '\n'){
            sort(vec.begin(), vec.end());
            for (string& str : vec)
                cout << str << " ";
            cout << endl;
            vec.clear();
        }
    }
    return 0;
}

字符串排序(3)

输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
输出描述:
对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格

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

using namespace std;

int main(){
    vector<string> vec;
    string str;
    while (getline(cin, str)) { // 解决 '\n'
        istringstream iss(str);
        while (getline(iss, str, ',')) // 按 ',' 分割
            vec.push_back(str);
        sort(vec.begin(), vec.end());
        for (int i = 0; i < vec.size() - 1; ++i)
            cout << vec[i] << ",";
        cout << vec.back() << endl;
        vec.clear();
    }
    return 0;
}

自测本地通过提交为0

数据范围: 0 < a , b < 2 × 1 0 10 0 < a,b < 2 \times 10^{10} 0<a,b<2×1010
输入描述:
输入有多组测试用例,每组空格隔开两个整数
输出描述:
对于每组数据输出一行两个整数的和;

#include <iostream>

using namespace std;

int main(){
    long long a, b;  // 需注意数据范围,样例通过不代表所有测试数据通过;
    while (cin >> a >> b)
        cout << a + b << endl;
    return 0;
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值