473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

about vector

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 ?

Jul 22 '05 #1
5 1311

"Rakesh Sinha" <ra***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Jul 22 '05 #2
Mike Wahler wrote:

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

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 .

Jul 22 '05 #3
Rakesh Sinha wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4

"Rakesh Sinha" <ra***********@yahoo.com> wrote in message
Mike Wahler wrote:

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

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.


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<MyClass> vt(1024, MyClass(2));
// 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
Jul 22 '05 #5
"Rakesh Sinha" <ra***********@yahoo.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
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.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
15
by: JustSomeGuy | last post by:
I have a need to make an applicaiton that uses a variable number of nested for loops. for now I'm using a fixed number: for (z=0; z < Z; ++z) for (y=0; y < Y; ++y) for (x=0; x < X; ++x)
4
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
18
by: Juha Kettunen | last post by:
Hi I don't know if I am using right words (bit-number), but this is what I mean: You can set a 64 bit number: unsigned long a; Now you can use binary operators to manipulate variable a...
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
by: Tom | last post by:
I was reading Bjourne's book and wonder about constructors? If I have a class template<class T> class Vector { public: explicit Vector(size_t n); } Does a default constructor get...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
7
by: Renzr | last post by:
I have a problem about the std::set<>iterator. After finding a term in the std::set<>, i want to know the distance from the current term to the begin(). But i have got a error. Please offer me...
7
by: Markus Pitha | last post by:
Hello again, I have another question about the handling of multidimensional vectors. Actually, it's not difficult when the size is known at the beginning, but I still wasn't able to create...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.