Explicitely specifying the size of a std::vector

M

mathieu

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;
}
 
D

Daniel T.

"mathieu said:
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?
 
J

Jonathan Mcdougall

mathieu said:
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
 
M

mathieu

Daniel said:
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
 
K

Kai-Uwe Bux

mathieu said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

vector 4
TF-IDF 1
Crossword 2
static_cast and std::vector 9
pointer to a vector 7
GET NEIL DEGRASSES TYSON, I ripped a hole with this one... 0
Correct usage of std::vector? 9
Crossword 14

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top