|
From: Ioannis V. <no...@ho...> - 2002-03-24 10:06:47
|
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Jason
Hardman
Sent: Sunday, March 24, 2002 2:33 AM
To: Dev-C++; Daniel Glenfield
Subject: Re: [Dev-C++] Struct tag
On the other hand the Vector class (std::vector<type>) is a C++
container class that mimics much of what an array is, and most likely
(although not guaranteed) is internally represented as an array or group
of arrays. On top of that the class supports automatic resizing and
bounds checking and a whole host of other manipulation methods and
algorithmic functions. This extra functionality will incur an extra
overhead on a system, but in modern C++ standard library implementations
this cost will be negligible, and unlikely to be significant. As a C++
coder more often than not you would be recommended to use a vector
although basic arrays will still have their uses occasionally.
Maybe this is more information that you wanted to know, but it may be
valuable in future use.
Jason.
sorrry for sqeezing the mateer so much but... :) a vector doesn't incur
any addtional cost when one knows how to use it. For example:
vector<int>array(1000);
for(vector<int>::size_type i=0; i<array.size(); i++)
// some stuff
Here no range checking occurs, the vector<int>::size() is inlined and
the object clears itself automatically when the end of its scope is
reached or an exception is thrown ("resource aquisition is
initialization" technique. That's why proper C++ doesn't need garbage
collection). Also the code is bullet proof (the probability of our
mistakes is very low) and we can change the initial size of the array by
changing only the value of the constructor.
This should not incur more cost than int *array=new int[1000];
for(size_t i=0; i<1000; i++)
// some stuff
When we want to avoid the cost of continuous new & delete we define a
large size from the beginning. Also it is guaranteed that subsequent
push_back operations do not incur time cost for every one push_back.
In summary if we know what we need and what we are doing everything is
ok. :)
For an example of efficicency of C++ high level containers and low level
C-style equivallents check my
http://users.forthnet.gr/ath/jvr/c++faq.htm
Ioannis
* Ioannis Vranos
* Programming pages: <http://www.noicys.d2g.com/>
http://www.noicys.d2g.com
* Alternative URL: <http://run.to/noicys> http://run.to/noicys
|