Explicitely specifying the size of a std::vector

Discussion in 'C++' started by mathieu, Aug 12, 2006.

  1. mathieu

    mathieu Guest

    Hello,

    I am looking at the API of std::vector but I cannot find a way to
    specify explicitely the size of my std::vector. I would like to avoid
    vector::resize since it first initializes the elements of the vector.

    Thank you !
    Mathieu

    Code:
    #include <sstream>
    #include <vector>

    int main()
    {
    int n = 2048*2048;
    std::vector<int> in;
    for(int i=0; i<n; ++i)
    {
    in.push_back( i );
    }
    std::stringstream ss;
    ss.write(reinterpret_cast<char*>(&in[0]), n*sizeof(int));

    std::vector<int> out;
    //out.resize( n );
    out.reserve( n );
    ss.read(reinterpret_cast<char*>(&out[0]), n*sizeof(int));

    return 0;
    }
    mathieu, Aug 12, 2006
    #1
    1. Advertising

  2. mathieu

    Daniel T. Guest

    In article <>,
    "mathieu" <> wrote:

    > Hello,
    >
    > I am looking at the API of std::vector but I cannot find a way to
    > specify explicitely the size of my std::vector. I would like to avoid
    > vector::resize since it first initializes the elements of the vector.
    >
    > Thank you !
    > Mathieu
    >
    > Code:
    > #include <sstream>
    > #include <vector>
    >
    > int main()
    > {
    > int n = 2048*2048;
    > std::vector<int> in;
    > for(int i=0; i<n; ++i)
    > {
    > in.push_back( i );
    > }
    > std::stringstream ss;
    > ss.write(reinterpret_cast<char*>(&in[0]), n*sizeof(int));
    >
    > std::vector<int> out;
    > //out.resize( n );
    > out.reserve( n );
    > ss.read(reinterpret_cast<char*>(&out[0]), n*sizeof(int));
    >
    > return 0;
    > }


    resize is your only choice in the above. Why are you trying to avoid it?
    I know you say above "because it first initializes the elements" but
    what's the problem with that?
    Daniel T., Aug 12, 2006
    #2
    1. Advertising

  3. mathieu wrote:
    > Hello,
    >
    > I am looking at the API of std::vector but I cannot find a way to
    > specify explicitely the size of my std::vector. I would like to avoid
    > vector::resize since it first initializes the elements of the vector.


    std::vector::resize() does construct elements if the specified size is
    greater than the value returned by std::vector::size(). However,
    std::vector::reserve() does not, it only makes sure no memory
    allocation will be made until size() would be greater than capacity().

    Note that reserve() does not allocate elements, which means you cannot
    access them with, for example, operator[]. Use must still use insertion
    functions such as push_back().


    Jonathan
    Jonathan Mcdougall, Aug 12, 2006
    #3
  4. mathieu

    mathieu Guest

    Daniel T. wrote:
    > resize is your only choice in the above. Why are you trying to avoid it?
    > I know you say above "because it first initializes the elements" but
    > what's the problem with that?


    Well...my though was the exact contrary :) Why would I first initialize
    my vector with values that I will immediately overwrite ?
    Running some benchmarks shows a 100% (resize if twice slower than
    reserve) percent difference if I use reserve and I maintain the size of
    the vector myself. So maybe the question now is : Is vector really
    adapted for my purpose ? What I am looking for is an efficient
    structure for loading (possibly multiples) array of byte from a file.
    It needs to be contiguous.

    Thanks,
    Mathieu
    mathieu, Aug 12, 2006
    #4
  5. mathieu

    Kai-Uwe Bux Guest

    mathieu wrote:

    >
    > Daniel T. wrote:
    >> resize is your only choice in the above. Why are you trying to avoid it?
    >> I know you say above "because it first initializes the elements" but
    >> what's the problem with that?

    >
    > Well...my though was the exact contrary :) Why would I first initialize
    > my vector with values that I will immediately overwrite ?
    > Running some benchmarks shows a 100% (resize if twice slower than
    > reserve) percent difference if I use reserve and I maintain the size of
    > the vector myself. So maybe the question now is : Is vector really
    > adapted for my purpose ? What I am looking for is an efficient
    > structure for loading (possibly multiples) array of byte from a file.
    > It needs to be contiguous.


    You could check whether your platform does some smart optimizations for
    streambuf_iterators:

    #include <ios>
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>

    typedef char byte;
    typedef std::vector< byte > byte_sequence;

    int main ( void ) {
    std::basic_fstream< byte > in_file
    ( "some_file.txt", std::ios::in | std::ios::binary );
    byte_sequence v ( ( std::istreambuf_iterator< byte >( in_file ) ),
    std::istreambuf_iterator< byte >() );
    std::copy( v.begin(), v.end(),
    std::eek:stream_iterator< byte >( std::cout ) );
    }

    In principle, the filesize is known at the time when the byte_sequence v is
    constructed. Thus, the compiler could generate code to allocate all memory
    needed at once. Whether it does so is a quality of implementation issue
    (and an issue of whether the file-size is really known, which may depend on
    the OS and the type of file).


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amelyan
    Replies:
    0
    Views:
    384
    Amelyan
    May 24, 2005
  2. Anonymous
    Replies:
    20
    Views:
    4,293
    Pete Becker
    Mar 30, 2005
  3. Jason Heyes
    Replies:
    8
    Views:
    721
    Andrew Koenig
    Jan 15, 2006
  4. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  5. Rune Allnor
    Replies:
    4
    Views:
    944
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page