在C ++中读取带有2个数字的行 [英] Reading lines with 2 numbers each in C++

查看:84
本文介绍了在C ++中读取带有2个数字的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++感到非常生锈.我想知道最好的方法是读取以下格式的输入:

I'm pretty rusty on my C++. I'm wondering what the best way is to read input in the following format:

400 200
138 493
...

我目前正在使用while(cin.peek()!=-1)检查EOF,然后在其中使用while(cin.peek()!='\n')检查换行符.这适合阅读整行文本,但是如何将其限制为2个数字和/或仅抓取这2个数字?

I'm currently using while(cin.peek()!=-1) to check for EOF, and then within that, I'm using while(cin.peek()!='\n') to check for newlines. This is fine for reading in full lines of text, but how can I limit it to 2 numbers and/or grab just those 2 numbers?

推荐答案

int num1,num2;
while(cin>>num1>>num2)
{
     //...
}

string line;
int num1,num2;
stringstream ss;
while(getline(cin,line))
{
    ss<<line;
    ss>>num1>>num2;
    //...
}

这篇关于在C ++中读取带有2个数字的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆