about vector

R

Rakesh Sinha

For a particular application of mine, I need to create a considerably
huge vector, initialized with the default constructor of a given class.


For eg the code fragment might look like -

vector<MyClass> vt ;
vt.reserve(1024); // some huge number

for (int i = 0 ; i < 1024 ; i++ )
vt.push_back (MyClass() );


Instead of doing it like this, is there any other API, that would be
look like -


vector<MyClass> CreateVector<MyClass>(1024) ?

This would return a vector<MyClass> of 1024 elements all initialized
with the default constructor and initialized *quickly* compared to the
previous methodology ?
 
M

Mike Wahler

Rakesh Sinha said:
For a particular application of mine, I need to create a considerably
huge vector, initialized with the default constructor of a given class.


For eg the code fragment might look like -

vector<MyClass> vt ;
vt.reserve(1024); // some huge number

for (int i = 0 ; i < 1024 ; i++ )
vt.push_back (MyClass() );


Instead of doing it like this, is there any other API, that would be
look like -


vector<MyClass> CreateVector<MyClass>(1024) ?

This would return a vector<MyClass> of 1024 elements all initialized
with the default constructor and initialized *quickly* compared to the
previous methodology ?

You're in luck, there's already a constructor that does what you
want:

std::vector<MyClass> vt(1024);

This requires that 'MyClass' provide a public default constructor.
However the 'quickness' is not specified by the language, but
is a "quality of implementation" issue.

-Mike
 
I

Ioannis Vranos

Rakesh said:
For a particular application of mine, I need to create a considerably
huge vector, initialized with the default constructor of a given class.


For eg the code fragment might look like -

vector<MyClass> vt ;
vt.reserve(1024); // some huge number

for (int i = 0 ; i < 1024 ; i++ )
vt.push_back (MyClass() );


Instead of doing it like this, is there any other API, that would be
look like -


vector<MyClass> CreateVector<MyClass>(1024) ?

This would return a vector<MyClass> of 1024 elements all initialized
with the default constructor and initialized *quickly* compared to the
previous methodology ?



You can simply do:


vector<MyClass> vt(1024);


This creates a vector containing 1024 objects of type MyClass which are
initialised with their default constructors.
 
R

Rakesh Sinha

You're in luck, there's already a constructor that does what you
want:

std::vector<MyClass> vt(1024);

Thanks. the solution is so simple.
This requires that 'MyClass' provide a public default constructor.

I was wondering if there is any facility in the language, by which
you can tell the compiler to use a particular constructor, specifically
instead of the default one. I did a quick search on google and
apparently could not find any.

Something like,

std::vector MyClass vt(1024, MyClass(int) ) ; //I would want
MyClass(int)
// to be used here.

I think the scenario is pretty much the same as here ..


MyClass * pt = new MyClass[10];
//Ok - this uses default. constructor only
// There is no way to specify a particular constructor here ??

delete [] pt;
However the 'quickness' is not specified by the language, but
is a "quality of implementation" issue.

Yeah. agreed. I use the implementation by Intel C++ compiler intel
8.1 .
I was wondering if there is any independent review on differnet
implementations with regard to STLs .
 
S

Sharad Kala

Rakesh Sinha said:
Thanks. the solution is so simple.


I was wondering if there is any facility in the language, by which
you can tell the compiler to use a particular constructor, specifically
instead of the default one. I did a quick search on google and
apparently could not find any.

There is a way, read on...
Something like,

std::vector MyClass vt(1024, MyClass(int) ) ; //I would want
MyClass(int)

Two problems
- std::vector<MyClass> (Note the angle brackets)
- MyClass(some_int_value)

std::vector said:
// to be used here.

I think the scenario is pretty much the same as here ..
MyClass * pt = new MyClass[10];
//Ok - this uses default. constructor only
// There is no way to specify a particular constructor here ??

delete [] pt;

It isn't as shown above.

Sharad
 
A

Andrew Koenig

std::vector MyClass vt(1024, MyClass(int) ) ; //I would want
MyClass(int)
// to be used here.

std::vector<MyClass> vt(1024, MyClass(42));

does almost what you want. However, instead of giving 42 as a constructor
argument to every element of vt, it constructs a single object with value
MyClass(42) and copy-constructs that argument into every element of vt.
 

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

Members online

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top