Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #5439
| From | Kai-Uwe Bux <jkherciueh@gmx.net> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: std::vector::reserve and std::ifstream::read |
| Followup-To | comp.lang.c++ |
| Date | 2011-05-24 20:33 +0200 |
| Organization | University of Bielefeld |
| Message-ID | <irgtls$84b$1@hoshi.visyn.net> (permalink) |
| References | (1 earlier) <397be274-fbc0-4692-b79a-f777bc5625e3@q30g2000vbs.googlegroups.com> <a5b5f98a-5d74-4457-8885-fff745f00465@p6g2000vbn.googlegroups.com> <1e00f7da-b08d-4a0d-9aa8-4421632af937@u26g2000vby.googlegroups.com> <irg6r3$ptr$1@newsreader5.netcologne.de> <f68fe7cc-ff71-40c1-b064-7c4e1d6c0154@z13g2000prk.googlegroups.com> |
Followups directed to: comp.lang.c++
mathieu wrote:
> On May 24, 2:03 pm, "Thomas J. Gritzan" <phygon_antis...@gmx.de>
> wrote:
>> Am 24.05.2011 13:51, schrieb mathieu:
>>
>>
>>
>>
>>
>> > On May 24, 1:01 pm, Michael Doubez<michael.dou...@free.fr> wrote:
>> >> On 24 mai, 11:16, gwowen<gwo...@gmail.com> wrote:
>>
>> >>> On May 24, 9:58 am, mathieu<mathieu.malate...@gmail.com> wrote:
>>
>> >>>> Dear all,
>>
>> >>>> I do not understand how to use the STL vector class with the
>> >>>> ifstream class. I would like to reserve a chunk of memory (no
>> >>>> initialization is required) and fill it with values from a file. As
>> >>>> far as I understand vector::reserve requires a subsequent call to
>> >>>> push_back or insert. However I do not see how I can do this in the
>> >>>> following example:
>>
>> >>>> #include<fstream>
>> >>>> #include<vector>
>>
>> >>>> int main(int argc, char *argv[])
>> >>>> {
>> >>>> const char *filename = argv[1];
>> >>>> std::ifstream is( filename );
>> >>>> const size_t l = 512;
>> >>>> std::vector<char> v;
>> >>>> v.reserve( l ); // need push_back or insert
>>
>> >>> If you reserve() space, you can't read or write to that space until
>> >>> you also adjust the size (as push_back() will, or resize()). The
>> >>> reserve() may well speed your code up.
>>
>> >>> char tmp;
>> >>> is.read(&tmp,1);
>> >>> v.push_back(tmp);
>>
>> >>> // the C++/STL like solution will use an istream_iterator and a
>> >>> back_inserter
>> >>> // I've got to say, I don't care for it...
>> >>> vector<char> V;
>> >>> copy(istream_iterator<char>(is), istream_iterator<char>(),
>> >>> back_inserter(V));
>>
>> >> Or simply:
>> >> v.assign(stream_iterator<char>(is), istream_iterator<char>());
>>
>> > It does not work for me. istream_iterator does not implement +()
>> > operator:
>>
>> > v.assign( std::istream_iterator<char>(is),
>> > std::istream_iterator<char>(is)+l);
>>
>> You are not supposed to do +x on this.
>>
>> istream_iterator<char>(is) is an iterator refering to the begin of the
>> stream while istream_iterator<char>() refers to the end of the stream,
>> just like c.begin() and c.end() for any container.
>> That pair of iterators reads from the stream until it hits the end of
>> the stream (like end of file).
>>
>> If you want to read into a fixed buffer of std::vector:
>>
>> std::ifstream is( filename );
>> const size_t size = 512;
>> std::vector<char> buffer(size);
>
> As said in my original post, I do not want to initialize the vector,
> simply because this should not be required.
Would it be fine, to construct the vector from a pair of istream_iterators?
std::vector< char > v
( std::istream_iterator<char>( is ), std::istream_iterator<char>() );
Now, this will put the _whole_ stream into the vector. If you only want part
of it and know how many characters, you could use
template < typename FromIter, typename ToIter, typename Int >
FromIter copy_n ( FromIter from, ToIter to, Int n ) {
for ( Int i = 0; i < n; ++i ) {
*from = *to;
++ from;
++ to;
}
return ( from );
}
and then
std::vector< char > v;
copy_n( std::istream_iterator< char >( is ),
std::back_inserter( v ),
the_length );
Best,
Kai-Uwe Bux
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
std::vector::reserve and std::ifstream::read mathieu <mathieu.malaterre@gmail.com> - 2011-05-24 01:58 -0700
Re: std::vector::reserve and std::ifstream::read gwowen <gwowen@gmail.com> - 2011-05-24 02:16 -0700
Re: std::vector::reserve and std::ifstream::read Michael Doubez <michael.doubez@free.fr> - 2011-05-24 04:01 -0700
Re: std::vector::reserve and std::ifstream::read mathieu <mathieu.malaterre@gmail.com> - 2011-05-24 04:51 -0700
Re: std::vector::reserve and std::ifstream::read "Thomas J. Gritzan" <phygon_antispam@gmx.de> - 2011-05-24 14:03 +0200
Re: std::vector::reserve and std::ifstream::read mathieu <mathieu.malaterre@gmail.com> - 2011-05-24 05:52 -0700
Re: std::vector::reserve and std::ifstream::read gwowen <gwowen@gmail.com> - 2011-05-24 06:42 -0700
Re: std::vector::reserve and std::ifstream::read Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-05-24 20:33 +0200
Re: std::vector::reserve and std::ifstream::read Joshua Maurice <joshuamaurice@gmail.com> - 2011-05-24 14:25 -0700
Re: std::vector::reserve and std::ifstream::read Michael Doubez <michael.doubez@free.fr> - 2011-05-24 05:06 -0700
Re: std::vector::reserve and std::ifstream::read mathieu <mathieu.malaterre@gmail.com> - 2011-05-24 05:53 -0700
Re: std::vector::reserve and std::ifstream::read Juha Nieminen <nospam@thanks.invalid> - 2011-05-24 16:14 +0000
Re: std::vector::reserve and std::ifstream::read Jeff Flinn <TriumphSprint2000@hotmail.com> - 2011-05-24 19:35 -0400
csiph-web