错误票据(stringstream)

Posted 中二病没有蛀牙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误票据(stringstream)相关的知识,希望对你有一定的参考价值。

这是一个语法题目,唯一的难点在怎么读入多行未知数据
https://www.acwing.com/problem/content/1206/

定义了三个类:istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作。

其用法可以如下:

template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100005;
int a[maxn];

int main()
{
    int n,l,ans1,ans2;
    cin>>n;
    string s;   
    getline(cin,s);
    l = 0;
    while(n--){
        getline(cin,s);
        stringstream ss(s);
        while(ss >> a[l]) l++;
    }
    sort(a,a+l);
    for(int i = 1; i <= l; i++){
        if(a[i] == a[i - 1])
            ans2 = a[i];
        if(a[i] == a[i - 1] + 2)
            ans1 = a[i] - 1; 
    }
    cout<<ans1<<" "<<ans2<<endl;
    return 0;
}

以上是关于错误票据(stringstream)的主要内容,如果未能解决你的问题,请参考以下文章

模拟特别数的和移动距离连号区间错误票据思路详解及代码实现

std::stringstream 错误?

[蓝桥杯] 错误票据

蓝桥杯-错误票据

错误票据

为啥类中的 stringstream 成员会导致编译时错误? [复制]