Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #5407

Re: std::vector::reserve and std::ifstream::read

From "Thomas J. Gritzan" <phygon_antispam@gmx.de>
Newsgroups comp.lang.c++
Subject Re: std::vector::reserve and std::ifstream::read
Date 2011-05-24 14:03 +0200
Organization news.netcologne.de
Message-ID <irg6r3$ptr$1@newsreader5.netcologne.de> (permalink)
References <eceb9b5c-af74-43f8-9946-25c53e6aff46@gh5g2000vbb.googlegroups.com> <397be274-fbc0-4692-b79a-f777bc5625e3@q30g2000vbs.googlegroups.com> <a5b5f98a-5d74-4457-8885-fff745f00465@p6g2000vbn.googlegroups.com> <1e00f7da-b08d-4a0d-9aa8-4421632af937@u26g2000vby.googlegroups.com>

Show all headers | View raw


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);
is.read(&buffer[0], size);

Check for errors after this and use is.gcount() to get the number of 
bytes read.

-- 
Thomas

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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