本文介绍了几个输入输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 当我将输入字符串中的内容复制到矢量时,我通常会使用 这样的东西 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< string>()"意思? 它定义了一个没有绑定到任何istream的迭代器吗?如果是这样,为什么我的 代码高于istream_iterator< stringit;不起作用? 有一些错误检测功能,例如bad(),good(), "失败()" ;.我不确定是什么构成了错误。如果我从 文件中读取并点击文件的末尾,它是否会使流出错? 状态?如果读数达到EOF,那么我需要使用clear()吗?要重置它吗?b $ b重置它?如果我使用clear()要重置一个流(无论出于什么错误 的原因),流上的剩余数据是否丢失了? 非常感谢! Jess 解决方案 它应该有效(除了你似乎已经使用了 变量名''它''两次)。你得到了什么错误? 什么是istream_iterator< string>()意思? 是 如果读数达到EOF,那么我是否需要使用clear()到 是的,但请注意,到达文件末尾与尝试读取文件末尾的不一样。只有后者才是错误。 如果我使用clear()重置一个流(对于任何错误 否 它应该有效(除了你似乎已经使用了 变量名''它''两次)。你得到了什么错误? 谢谢,我再试一次,这个错误就消失了。 :)顺便说一句, 似乎两者都是istream_iterator it();和istream_iterator it; 工作,有什么不同吗? Jess 当我将内容从输入字符串复制到向量时,我通常使用 这样的东西,如 它应该有用(除了你似乎已经使用了变量名''它''两次)。你得到了什么错误? 谢谢,我再试一次,这个错误就消失了。 :)顺便说一句, 似乎两者都是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 usesomething like thisvector<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 didistream_iterator<stringit(in);this works, but there''s no way I can define something equivalent toistream_iterator<string>() above. I triedistream_iterator<stringit;and this failed. What does "istream_iterator<string>()" mean? Doesit define an iterator that''s not bound to any istream? If so, why mycode 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 afile and hit the end of the file, does it leave the stream in an errorstate? If the reading reaches EOF, then do I need to use "clear()" toreset it? If I use "clear()" to reset a stream (for whatever errorreasons), are the remaining data on the stream lost?Thanks a lot!Jess 解决方案It should work (apart from the fact that you seem to have used thevariable name ''it'' twice). What error did you get?What does "istream_iterator<string>()" mean? DoesYesIf the reading reaches EOF, then do I need to use "clear()" toYes, but be careful here, reaching the end of file is not the same astrying to read past the end of file. Only the latter is an error.If I use "clear()" to reset a stream (for whatever errorNoIt should work (apart from the fact that you seem to have used thevariable 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 It should work (apart from the fact that you seem to have used thevariable 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, whereasistream_iterator it;defines it as an istream_iterator object and calls its default constructor.--rbh 这篇关于几个输入输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-22 11:54
查看更多