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 ? 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
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 .
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
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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)
|
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;
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |