istringstream - 如何做到这一点? [英] istringstream - how to do this?

查看:110
本文介绍了istringstream - 如何做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个档案:

a 0 0
b 1 1
c 3 4
d 5 6

使用istringstream,我需要得到a,然后b,然后c等。我不知道该怎么做,因为在网上或在我的书里没有很好的例子。

Using istringstream, I need to get a, then b, then c, etc. But I don't know how to do it because there are no good examples online or in my book.

到目前为止的代码:

ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
iss >> id;

这两次都输出id为a我不知道如何使用istringstream显然和我HAVE使用istringstream。请帮助!

This prints "a" for id both times. I don't know how to use istringstream obviously and I HAVE to use istringstream. Please help!

推荐答案

ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
istringstream iss2(line);
iss2 >> id;

getline(file,line);
iss.str(line);
iss >> id;

istringstream 复制您提供的字符串。它看不到对的更改。要么构造一个新的字符串流,要么强制它采用新的字符串副本。

istringstream copies the string that you give it. It can't see changes to line. Either construct a new string stream, or force it to take a new copy of the string.

这篇关于istringstream - 如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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