0

Possible Duplicate:
Using arrays or std::vectors in C++, what's the performance gap?

I just wanna know which of them are the faster and use less resources? I think that vector is more reliable and secure but a pointer to an array is faster. I want to re-size the array (add new elements, so increment it by 1 or remove elements from it). A vector has its functions for that while a pointer needs one created by me.

I don't know which one to choose. What do you advise me? Thanks!

Community
  • 1
  • 1
ali
  • 10,927
  • 20
  • 89
  • 138
  • 12
    `"I want to re-size the array"` - Use a `vector`... end of story. – Mysticial Jul 27 '12 at 16:30
  • 1
    Alwyes use vector if you want to have a "re-sizeable" array. It's the best implementation for that common problem, don't redo the work! – Mithrandir Jul 27 '12 at 16:31
  • 3
    As is the case with all such questions, the answer is: write clean, simple code first, then after you have it working, see if it is fast enough. If it isn't, determine which parts of the code are causing performance problems (by profiling) and improve the performance of those parts. Start with `vector`, but design your program in such a way that you could easily swap it out for any other random access sequence if you later determine that you need to do so. – James McNellis Jul 27 '12 at 16:32
  • Classic newbie concern. Don't you know that computers are really fast? As James says, do it the easy way first, then if you have efficiency concerns optimize later. But in this case vector is liable to be as fast as any code you could write anyway. – jahhaj Jul 27 '12 at 17:06

2 Answers2

14

According to Bjarne Stroustrup, you should use vector over Array unless you have a really good reason to use an array.

nurettin
  • 11,090
  • 5
  • 65
  • 85
Caesar
  • 9,483
  • 8
  • 40
  • 66
8

The c++ standard libaries have been optimized to be as fast as possible all the while providing necessary functions so that you do not have to implement them. Save yourself the time and worry and just use a vector.

If there are any discrepancies on speed they will be negiligble in the big picture :)

zanegray
  • 768
  • 7
  • 13