使用 istream_iterator 并从标准输入或文件读取
我正在使用 Microsoft Visual C++ 进行编写,我希望我的程序能够从标准输入或使用 istream_iterator 的文件中读取数据。谷歌搜索互联网并没有显示出我认为它有多简单。例如,我可以很容易地编写这个并从标准输入中读取:
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<string> my_it(cin);
for (; my_it != istream_iterator<string>(); my_it++)
printf("%s\n", (*my_it).c_str());
}
或者我可以编写这个并从文件中读取:
#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ifstream file(argv[1]);
istream_iterator<string> my_it(file);
for (; my_it != istream_iterator<string>(); my_it++)
printf("%s\n", (*my_it).c_str());
}
但是我如何将这两者结合起来,以便一个简单的 (argc == 2)
check 让我可以使用文件流或标准输入初始化输入流迭代器,然后继续我的快乐之路?
I'm writing in Microsoft Visual C++ and I'd like my program to either read from standard input or a file using the istream_iterator
. Googling the internets hasn't shown how simple I think it must be. So for example, I can write this pretty easily and read from standard input:
#include <iostream>
#include <string>
#include <iterator>
using namespace std;
int main()
{
istream_iterator<string> my_it(cin);
for (; my_it != istream_iterator<string>(); my_it++)
printf("%s\n", (*my_it).c_str());
}
Or I can write this and read from a file:
#include <iostream>
#include <string>
#include <iterator>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
ifstream file(argv[1]);
istream_iterator<string> my_it(file);
for (; my_it != istream_iterator<string>(); my_it++)
printf("%s\n", (*my_it).c_str());
}
But how do I combine these two so that a simple (argc == 2)
check lets me initialize my input stream iterator with either a file stream or stdin and go on about my merry way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在构造迭代器后对其进行赋值:
You can assign to the iterator after constructing it:
乍一看,最简单的解决方案是使用三元运算符
?:
,如下所示:但是,这不太有效,因为它构造了一个临时
ifstream
对象,该对象将在声明结束时被销毁。因此,您需要一种有条件地创建ifstream
并在for
循环后有条件地销毁它的方法。std::auto_ptr
符合要求。因此:一个不同的、可能更干净的解决方案是将迭代移动到一个采用 istream& 的单独函数。
我之前在网上见过这个问题,由一位 C++ 大师介绍过。不幸的是,我不记得具体在哪里,或者是谁做的!我想是在 DDJ 上,也许是 Sutter 或 Alexandrescu?
At first glance, the simplest solution would be to use the ternary operator
?:
like this:However, that won't quite work because it constructs a temporary
ifstream
object, which will be destroyed at the end of the statement. So you need a way of conditionally creating anifstream
, and conditionally destroying it after thefor
loop.std::auto_ptr<>
fits the bill. Thus:A different, probably cleaner solution would be to move the iteration to a separate function that takes
istream&
.I've seen this problem before online, covered by one of the C++ greats. Unfortunately, I don't remember exactly where, or by whom! I think it was on DDJ, maybe Sutter or Alexandrescu?
这个小片段将为您提供一个 istream
输入
,它可以是文件或 std::cin。您现在可以使用
input
,而不必担心输入来自哪个源。This small snippet will give you an istream
input
that can be either a file or std::cin.You may now use
input
without worrying which source the input is coming from.你的意思是这样的吗:(使用指针)
我还没有测试过这个。这就是你所追求的吗?
Do you mean something like this: (using pointers)
I haven't tested this, though. Is that sort of what you're after?