从一行中解析整数 [英] Parsing integers from a line

查看:109
本文介绍了从一行中解析整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析输入文本文件.如果我使用getline()一次抓取输入的一行,是否有一种方法可以搜索字符串以获取整数?我在想类似于Java中的getNextInt()的方法.

I am parsing an input text file. If I grab the input one line at a time using getline(), is there a way that I can search through the string to get an integer? I was thinking something similar to getNextInt() in Java.

我知道该输入行中必须有2个数字;但是,这些值将由一个或多个空格字符分隔,所以我不能只转到特定位置.

I know there has to be 2 numbers in that input line; however, these values will be separated by one or more white space characters, so I can't just go to a specific position.

推荐答案

如果其中只有空格和整数,请尝试如下操作:

If the only thing in there is whitespace and integers, just try something like this:

int i1, i2;
stringstream ss(lineFromGetLine);
ss >> i1 >> i2;

或更简单:

int i1, i2;
theFileStream >> i1 >> i2;

这篇关于从一行中解析整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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