|
-
March 15th, 2005, 07:35 PM
#1
best C++ practice... vector or []
Hi to all,
first let me say i'm sorry if this has been asked in the forum already, i did some searches but i haven't found any real results.
My question is if it's good practice in C++ to use the STL classes instead of other methods, like is it better to use a std::vector<int> or a simple int[] for arrays?
thanks for the help in advance. and once again sorry if this has been asked before
Alex Paes
-
March 15th, 2005, 07:50 PM
#2
Re: best C++ practice... vector or []
Most people would say std::vector<int>. I tend to use int[] only if it's a very simple problem. In general, std::vector is the safer choice.
Kelly
-
March 15th, 2005, 11:28 PM
#3
Re: best C++ practice... vector or []
std::vector<int> has some advantages over int[]. vectors has no upper limit for inserting or storing data but for arrays you are buond to some limit.
-
March 16th, 2005, 03:19 AM
#4
Re: best C++ practice... vector or []
std::vector<> takes care of life-time management. It provides useful function (such as size()) and you can pass it as a return value.
std::vector<> has many advantages over a plain []. The only disadvantage I see is that sometimes you don't want the flexibility of changing the size of the std::vector. In those situations I use (http://www.boost.org/doc/html/array.html).
If I see plain C-arrays ([] ) in my code I tend to refactor it into std::vector/boost::array.
So, IMO the best practice is to use std::vector as the default choice.
-
March 16th, 2005, 03:38 AM
#5
Re: best C++ practice... vector or []
In most situations, best practice is to use std::vector. Because it gives extended functionalities for use, like swap(int x, int y) for swaping two elements and at(int n) which returns the element at index n, with bounds checking (releiving u from the run-time crashes ).
[] operator is still provided with Vector but it doesn't provide bounds checking.
There are some performance penalties also, as at() returns an elements with bounds checking, but at an additionally cost.
Use vector when u really needs these, array otherwise.
Cheers!!!
Mukesh Vijay
-
March 16th, 2005, 06:44 AM
#6
Re: best C++ practice... vector or []
thanks everyone for your repplies,
i decided to go for the std::vector approach mainly because i have 2 arrays that need resizing and 1 that doesn't so using std::vector even for the one that doesn't seems logical if not, only to keep the same coding style as the other 2 arrays.
it just seems to me that std::vector has so many advantages that i was feeling it might be a lot slower than standard [] arrays. Has any of you guys noticed any performance hit using std::vector?
cheers
Alex Paes
-
March 16th, 2005, 08:03 AM
#7
Re: best C++ practice... vector or []
The performance is very good, typically the code is inlined and optimized away leaving little or no additional cost. The only thing to consider is that the elements are always allocated on the heap, so a stack based array may be faster in rare situations where the array is constructed/destroyed rapidly.
-
March 16th, 2005, 09:17 AM
#8
Re: best C++ practice... vector or []
 Originally Posted by bmoodie
The performance is very good, typically the code is inlined and optimized away leaving little or no additional cost. The only thing to consider is that the elements are always allocated on the heap, so a stack based array may be faster in rare situations where the array is constructed/destroyed rapidly.
Or you just use boost::array which is stack based and has a std::vector-like interface.
-
March 16th, 2005, 05:56 PM
#9
Re: best C++ practice... vector or []
 Originally Posted by [email protected]
In most situations, best practice is to use std::vector. Because it gives extended functionalities for use, like swap(int x, int y) for swaping two elements and at(int n) which returns the element at index n, with bounds checking (releiving u from the run-time crashes  ).
[] operator is still provided with Vector but it doesn't provide bounds checking.
There are some performance penalties also, as at() returns an elements with bounds checking, but at an additionally cost.
Use vector when u really needs these, array otherwise.
Cheers!!!
Actually, if you use an iterator to access a vector container, it will out perform the C-Style type[] array.
So if used correctly, the vector will out perform the C-Style array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|