🔒 Locked
vector template
Advertisement
Original Post
I was working opn a code and didnt understand what that vector meant(it was a 3d game code so i thought it really was a vector). However when i first understand what it does it instantly occured to me - is this gods gift to programmers? i mean we dont have to worry about memory allocation and in other aspects it works perfectly as an array. Now comes the question part - Are there any bad sides to it? Is it much more computationally expensive than normal pointers?
Depending on your use of
Crispy
vector, it can be slow relative to other STL template classes, such as list, deque, etc. For example, vector performs appallingly comapred to list if you do a lot of deleteion, insertions, etc in the middle of it. For more specific info search the web or this forum - this topic has been discussed in several threads earlier.Crispy
http://www.gamedev.net/community/forums/topic.asp?topic_id=88402
http://www.gamedev.net/community/forums/topic.asp?topic_id=101552
http://www.gamedev.net/community/forums/topic.asp?topic_id=77880
http://www.gamedev.net/community/forums/topic.asp?topic_id=52654
Can''t seem to be able to find any links posted in this forum that could give you a better understanding of STL classes...
PS - I didn''t (re)read those threads, so they may be a little off
Hope this helps,
Crispy
http://www.gamedev.net/community/forums/topic.asp?topic_id=101552
http://www.gamedev.net/community/forums/topic.asp?topic_id=77880
http://www.gamedev.net/community/forums/topic.asp?topic_id=52654
Can''t seem to be able to find any links posted in this forum that could give you a better understanding of STL classes...
PS - I didn''t (re)read those threads, so they may be a little off
Hope this helps,
Crispy
quote:
Velochy
I was working opn a code and didnt understand what that vector meant(it was a 3d game code so i thought it really was a vector). However when i first understand what it does it instantly occured to me - is this gods gift to programmers? i mean we dont have to worry about memory allocation and in other aspects it works perfectly as an array.
Yes, the standard vector class is quite useful.
There are other container classes in the C++ standard template library (STL) which you might want to check out. Examples are: string, list, map, hash_map, set, etc. However, the library is not just limited to container classes; there are a variety of algorithms, an exception class hierarchy, etc.
SGI has a relatively good reference on the STL. MSDN''s online reference is slightly more accurate but a little bit more abstract and technical. The reason for this is that SGI''s reference refers to a newer, experimental version of the library that is not yet standard. I would like to add that if you have an older compiler (circa VC++ 6.0) there may be some problems with the library.
quote:
Original post by Velochy
Now comes the question part - Are there any bad sides to it? Is it much more computationally expensive than normal pointers?
Vector has a few more bytes of overhead for the class, but in most cases this is negligible. It stores its data in a C-style array, with zero overhead per element. Nevertheless, the C++ standard doesn''t guarantee the underlying implementation of the vector is an array, so you can''t use it like the following:
# include <vector>
using namespace std;
int main ()
{
// Create a buffer of bytes, this is good so far.
vector <char> buffer (1024 * 64);
// Bad! This assumes that buffer''s elements are contiguous!
some_C_function ((void*) &buffer [0]);
}
Personally, I don''t understand why the standard does not make an exception for std::vector. There is no way to implement vector with reasonable efficiency using anything else than a single C-style array, nor does abstracting the underlying representation have any practical benefit to users of vector.
Most C++ code either does not assume the elements are contiguous or can be rewritten generically (i.e., using templates) to avoid making this assumption. So yes, despite this one flaw, vector can be extremely useful.
quote:
Original post by Crispy
Depending on your use of vector, it can be slow relative to other STL template classes, such as list, deque, etc. For example, vector performs appallingly comapred to list if you do a lot of deleteion, insertions, etc in the middle of it. For more specific info search the web or this forum - this topic has been discussed in several threads earlier.
I think he asking about the performance of vector relative to C-style arrays, not other container classes.
quote:
Nevertheless, the C++ standard doesn''t guarantee the underlying implementation of the vector is an array, so you can''t use it like the following:
Uhh... Maybe doesn''t it guarantee that the implementation is an array, but I know it does guarantee that the elements are stored contiguously, so in effect, you can take the address of the first element and still be on the safe side.
AFAIK, that''s why Herb Sutter (www.gotw.ca) recommends using deque instead of vector as the default container. It doesn''t have to store the elements contiguously, so it is generally more efficient.
Cédric
quote:
Original post by Crispy
For example,vectorperforms appallingly comapred tolistif you do a lot of deleteion, insertions, etc in the middle of it.
Of course, the same goes for a raw array.
"The churches used to win their arguments against atheism, agnosticism, and other burning issues by burning the ismists, which is fine proof that there is a devil but hardly evidence that there is a God."
Ben Lindsey and Wainwright Evans
quote:
Original post by cedricl
Uhh... Maybe doesn''t it guarantee that the implementation is an array, but I know it does guarantee that the elements are stored contiguously, so in effect, you can take the address of the first element and still be on the safe side.
Nope.
http://www.gotw.ca/publications/mill10.htm
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#69
You can of course make the assumption that vector will store its elements contiguously. Hopefully the defect report will be addressed in the next version of the C++ standard, but for now the fact is that the standard does not guarantee vector''s elements are stored contiguously.
Herb Sutter says this himself:
The only (minor, and as you''ll soon see, pretty much moot) fly in the ointment is that the C++ Standard as it''s currently written doesn''t guarantee that Example 1(c) will actually work portably on every C++ platform you might buy. The reason is pretty simple: Example 1(c) assumes that the internal contents of the vector are stored contiguously in the same format as is an array, but today''s Standard doesn''t require vendors to implement vector that way. If, for example, a vector implementation were to play games and store its contents in some other way (such as contiguously but in backwards order, or in sequential order but with an extra byte of housekeeping information between elements), then Example 1(c) would in fact fail miserably.
quote:
Original post by cedricl
AFAIK, that''s why Herb Sutter (www.gotw.ca) recommends using deque instead of vector as the default container. It doesn''t have to store the elements contiguously, so it is generally more efficient.
No, he''s making an assumption about the implementation of std::vector. Please see the above links for clarification.
Also, I think his reason for using deque as the default container leans more towards ease of use than efficiency:
So, for the three reasons cited earlier: Consider preferring deque by default in your programs, especially when the contained type is a class or struct and not a builtin type, unless the actual performance difference in your situation is known to be important or you specifically require the contained elements to be contiguous in memory (which typically means that you intend to pass the contents to a function that expects an array).
quote:
Original post by cedricl
Uhh... Maybe doesn't it guarantee that the implementation is an array, but I know it does guarantee that the elements are stored contiguously, so in effect, you can take the address of the first element and still be on the safe side.
Original post by null_pointer
Nope.
http://www.gotw.ca/publications/mill10.htm
http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-defects.html#69
Right. My mistake. Sorry.
quote:
You can of course make the assumption that vector will store its elements contiguously. Hopefully the defect report will be addressed in the next version of the C++ standard, but for now the fact is that the standard does not guarantee vector's elements are stored contiguously.
Herb Sutter says this himself:
The only (minor, and as you'll soon see, pretty much moot) fly in the ointment is that the C++ Standard as it's currently written doesn't guarantee that Example 1(c) will actually work portably on every C++ platform you might buy. The reason is pretty simple: Example 1(c) assumes that the internal contents of the vector are stored contiguously in the same format as is an array, but today's Standard doesn't require vendors to implement vector that way. If, for example, a vector implementation were to play games and store its contents in some other way (such as contiguously but in backwards order, or in sequential order but with an extra byte of housekeeping information between elements), then Example 1(c) would in fact fail miserably.
Original post by cedricl
AFAIK, that's why Herb Sutter (www.gotw.ca) recommends using deque instead of vector as the default container. It doesn't have to store the elements contiguously, so it is generally more efficient.
Original post by null_pointer
No, he's making an assumption about the implementation of std::vector. Please see the above links for clarification.
Allow me to quote the very article you linked to. The Summary part:
It's safe to use vectors interchangeably with C-style arrays, and you should always prefer to use vectors or deques instead of arrays. Get in the habit of always using deque instead of vector whenever you don't need all of the contained elements to be contiguous in memory.
quote:
Also, I think his reason for using deque as the default container leans more towards ease of use than efficiency:
Maybe so. See the benchmarks:
http://www.gotw.ca/gotw/054.htm
For some operations, deques are faster, and they are not that much slower for others.
Anyway, I would say to the OP: make a typedef, and benchmark all three solutions (vectors, deques and lists).
Cédric
[edited by - cedricl on July 21, 2002 4:07:59 PM]
quote:
Original post by cedricl
Allow me to quote the very article you linked to. The Summary part:
It's safe to use vectors interchangeably with C-style arrays, and you should always prefer to use vectors or deques instead of arrays. Get in the habit of always using deque instead of vector whenever you don't need all of the contained elements to be contiguous in memory.
What is your point?
I haven't the faintest idea what you are arguing about. The fact that the standard does NOT GUARANTEE that vector's elements are always contiguous demonstrates that Sutter is making an ASSUMPTION about the way vectors are implemented. That is simple logic.
quote:
Original post by cedricl
Maybe so. See the benchmarks:
http://www.gotw.ca/gotw/054.htm
For some operations, deques are faster, and they are not that much slower for others.
I read the benchmarks before I made my previous post. For ONE operation, deque was faster than vector. For most other operations, vector was noticably faster. The deque he tested was about 400% slower for traversals with both intrinsic and user-defined types.
Sure, for lots of insertions and removals in the middle of the sequence, non-contiguous containers have a definite advantage. But anyone using an array for that should be, well, shot.
[edited by - null_pointer on July 21, 2002 5:33:30 PM]
quote:
Original post by null_pointer
What is your point?
I haven''t the faintest idea what you are arguing about. The fact that the standard does NOT GUARANTEE that vector''s elements are always contiguous demonstrates that Sutter is making an ASSUMPTION about the way vectors are implemented. That is simple logic.
My point is exactly what was presented in the summary.
Sutter says It''s safe to use vectors interchangeably with C-style arrays, and you should always prefer to use vectors or deques instead of arrays. I don''t have the knowledge to argue with him, and IMHO, it makes sense.
If that was what you were saying all along, my apologies for the misunderstanding.
Cédric
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement