Click to See Complete Forum and Search --> : best C++ practice... vector or []


AlexPaes
March 15th, 2005, 06:35 PM
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

Runt888
March 15th, 2005, 06:50 PM
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

jawadhashmi
March 15th, 2005, 10:28 PM
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.

marten_range
March 16th, 2005, 02:19 AM
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.

mukeshvijay79@hotmail.com
March 16th, 2005, 02:38 AM
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!!!

AlexPaes
March 16th, 2005, 05:44 AM
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

bmoodie
March 16th, 2005, 07:03 AM
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.

marten_range
March 16th, 2005, 08:17 AM
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.

Axter
March 16th, 2005, 04:56 PM
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.