几个输入输出问题 [英] several input-output questions

查看:63
本文介绍了几个输入输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


当我将输入字符串中的内容复制到矢量时,我通常会使用

这样的东西


vector< stringv;

ifstream in(" file");

copy(istream_iterator< string(in),istream_iterator< string>(),

back_inserter(v));


但是,我也尝试单独定义istream_iterator。我做了


istream_iterator< stringit(in);


这个有效,但是我无法定义相当于<的东西br />
istream_iterator< string>()上面。我试过了


istream_iterator< stringit;


这次失败了。什么是'istream_iterator< string>()"意思?

它定义了一个没有绑定到任何istream的迭代器吗?如果是这样,为什么我的

代码高于istream_iterator< stringit;不起作用?


有一些错误检测功能,例如bad(),good(),

"失败()" ;.我不确定是什么构成了错误。如果我从

文件中读取并点击文件的末尾,它是否会使流出错?

状态?如果读数达到EOF,那么我需要使用clear()吗?要重置它吗?b $ b重置它?如果我使用clear()要重置一个流(无论出于什么错误

的原因),流上的剩余数据是否丢失了?


非常感谢!

Jess

解决方案

Jess写道:


您好,


当我将内容从输入字符串复制到矢量时,我通常使用

这样的东西


vector< ; stringv;

ifstream in(" file");

copy(istream_iterator< string(in),istream_iterator< string>(),

back_inserter(v));


但是,我也尝试单独定义istream_iterator。我做了


istream_iterator< stringit(in);


这个有效,但是我无法定义相当于<的东西br />
istream_iterator< string>()上面。我试过了


istream_iterator< stringit;


这次失败了。



它应该有效(除了你似乎已经使用了

变量名''它''两次)。你得到了什么错误?


什么是istream_iterator< string>()意思?


它是否定义了一个未绑定到任何istream的迭代器?如果是这样,为什么我的

代码高于istream_iterator< stringit;不起作用?


有一些错误检测功能,例如bad(),good(),

"失败()" ;.我不确定是什么构成了错误。如果我从

文件中读取并点击文件的末尾,它是否会使流出错?

状态?






如果读数达到EOF,那么我是否需要使用clear()到


重置它?



是的,但请注意,到达文件末尾与尝试读取文件末尾的
不一样。只有后者才是错误。


如果我使用clear()重置一个流(对于任何错误


原因),流上的剩余数据是否丢失?





>

非常感谢!

Jess


6月17日晚上11点16分,John Harrison< john_androni ... @ hotmail.com>

写道:


Jess写道:


你好,


当我将内容从输入字符串复制到矢量时,我通常使用

这样的东西,如


vector< stringv;

ifstream in(" file");

copy(istream_iterator< string(in),istream_iterator< string>(),

back_inserter(v));


但是,我也尝试单独定义istream_iterator。我做了


istream_iterator< stringit(in);


这个有效,但是我无法定义相当于
的东西
istream_iterator< string>()以上。我试过


istream_iterator< stringit;


这个失败了。



它应该有效(除了你似乎已经使用了

变量名''它''两次)。你得到了什么错误?



谢谢,我再试一次,这个错误就消失了。 :)顺便说一句,

似乎两者都是istream_iterator it();和istream_iterator it;

工作,有什么不同吗?

Jess


Jess写道:


6月17日晚上11点16分,John Harrison< john_androni ... @ hotmail.com>

写道:


> Jess写道:


你好,

< blockquote class =post_quotes>
当我将内容从输入字符串复制到向量时,我通常使用

这样的东西,如


vector< stringv;

ifstream in(" file");

copy(istream_iterator< string(in),istream_iterator< string>( ),

back_inserter(v));


但是,我也尝试单独定义istream_iterator。我做了


istream_iterator< stringit(in);


这个有效,但是我无法定义相当于
的东西
istream_iterator< string>()以上。我试过


istream_iterator< stringit;


这个失败了。


它应该有用(除了你似乎已经使用了
变量名''它''两次)。你得到了什么错误?



谢谢,我再试一次,这个错误就消失了。 :)顺便说一句,

似乎两者都是istream_iterator it();和istream_iterator it;

工作,有什么区别吗?



istream_iterator it();


将其声明为返回istream_iterator的函数,而


istream_iterator it;


将其定义为istream_iterator对象并调用其默认构造函数。


-

rbh


Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_iterator<string(in), istream_iterator<string>(),
back_inserter(v));

However, I also tried to define istream_iterator separately. I did

istream_iterator<stringit(in);

this works, but there''s no way I can define something equivalent to
istream_iterator<string>() above. I tried

istream_iterator<stringit;

and this failed. What does "istream_iterator<string>()" mean? Does
it define an iterator that''s not bound to any istream? If so, why my
code above "istream_iterator<stringit;" doesn''t work?

There are some error-detection functions, such as "bad(), "good()",
"fail()". I''m not sure what constitutes "errors". If I read from a
file and hit the end of the file, does it leave the stream in an error
state? If the reading reaches EOF, then do I need to use "clear()" to
reset it? If I use "clear()" to reset a stream (for whatever error
reasons), are the remaining data on the stream lost?

Thanks a lot!
Jess

解决方案

Jess wrote:

Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_iterator<string(in), istream_iterator<string>(),
back_inserter(v));

However, I also tried to define istream_iterator separately. I did

istream_iterator<stringit(in);

this works, but there''s no way I can define something equivalent to
istream_iterator<string>() above. I tried

istream_iterator<stringit;

and this failed.

It should work (apart from the fact that you seem to have used the
variable name ''it'' twice). What error did you get?

What does "istream_iterator<string>()" mean? Does

it define an iterator that''s not bound to any istream? If so, why my
code above "istream_iterator<stringit;" doesn''t work?

There are some error-detection functions, such as "bad(), "good()",
"fail()". I''m not sure what constitutes "errors". If I read from a
file and hit the end of the file, does it leave the stream in an error
state?

Yes

If the reading reaches EOF, then do I need to use "clear()" to

reset it?

Yes, but be careful here, reaching the end of file is not the same as
trying to read past the end of file. Only the latter is an error.

If I use "clear()" to reset a stream (for whatever error

reasons), are the remaining data on the stream lost?

No

>
Thanks a lot!
Jess


On Jun 17, 11:16 pm, John Harrison <john_androni...@hotmail.com>
wrote:

Jess wrote:

Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_iterator<string(in), istream_iterator<string>(),
back_inserter(v));

However, I also tried to define istream_iterator separately. I did

istream_iterator<stringit(in);

this works, but there''s no way I can define something equivalent to
istream_iterator<string>() above. I tried

istream_iterator<stringit;

and this failed.


It should work (apart from the fact that you seem to have used the
variable name ''it'' twice). What error did you get?

Thanks, I tried it again, and this error disappears. :) By the way,
it seems both "istream_iterator it();" and "istream_iterator it;"
work, is there any difference?
Jess


Jess wrote:

On Jun 17, 11:16 pm, John Harrison <john_androni...@hotmail.com>
wrote:

>Jess wrote:

Hello,

When I copy contents from an input string to a vector, I typically use
something like this

vector<stringv;
ifstream in("file");
copy(istream_iterator<string(in), istream_iterator<string>(),
back_inserter(v));

However, I also tried to define istream_iterator separately. I did

istream_iterator<stringit(in);

this works, but there''s no way I can define something equivalent to
istream_iterator<string>() above. I tried

istream_iterator<stringit;

and this failed.


It should work (apart from the fact that you seem to have used the
variable name ''it'' twice). What error did you get?


Thanks, I tried it again, and this error disappears. :) By the way,
it seems both "istream_iterator it();" and "istream_iterator it;"
work, is there any difference?

istream_iterator it();

declares it as a function returning an istream_iterator, whereas

istream_iterator it;

defines it as an istream_iterator object and calls its default constructor.

--
rbh


这篇关于几个输入输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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