views:

125

answers:

2

How can I create and manipulate a vector of ifstreams?

Something like this, except this doesn't work:

vector<ifstream> Files(10, ifstream());
Files[0].open("File");
+9  A: 

You cannot store ifstreams in a std::vector, because you can not create copies of them.

You can accomplish something similar by storing pointers instead. In that case, I recommend you use some sort of a pointer container to make sure that those ifstreams get deleted.

hrnt
+1: ...although without knowing the context it's hard to say whether a smart pointer is really required. That might just be complicating things.
Troubadour
+3  A: 

The closest I can think of is vector<shared_ptr<ifstream> > — you can't put ifstreams in vector as they're not copy-constructible.

Michael Krelin - hacker
Just an interesting tid-bit: in C++ 0x, we will be able to put streams in containers, because even though they're not copy-constructible, they will be move-constructible.
Jerry Coffin
Jerry, thanks, I know I should take a closer look at this brave new world, but I keep postponing it. (P.S. I also started with Fortran IV:))
Michael Krelin - hacker
Given that we are running out of decimal values of 'x' in 'C++ 0x', I think it's fair to say that you're not the only person doing the postponing, hacker. ;)
Kylotan
That's a great consolation, Kylotan ;-)
Michael Krelin - hacker
BTW, Kylotan, shouldn't values of `x` in `0x` be octal, not decimal?
Michael Krelin - hacker