Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicitely specifying the size of a std::vector

Reply
Thread Tools

Explicitely specifying the size of a std::vector

 
 
mathieu
Guest
Posts: n/a
 
      08-12-2006
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;
}

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      08-12-2006
In article <(E-Mail Removed) .com>,
"mathieu" <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      08-12-2006

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

 
Reply With Quote
 
mathieu
Guest
Posts: n/a
 
      08-12-2006

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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-12-2006
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: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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Any exit status without explicitely using return /exit Keith Thompson C Programming 10 03-03-2010 12:41 AM
Re: Any exit status without explicitely using return /exit jacob navia C Programming 3 02-24-2010 02:39 PM
Must explicitely the ID of control? Why? Amelyan ASP .Net 0 05-23-2005 11:50 PM
Calling constructor explicitely Victor Bazarov C++ 9 02-24-2005 11:28 AM
Explicitely calling Thread.run Phaedrus Java 3 07-06-2003 07:28 PM



Advertisments