Ryan.
Ryan.

Reputation: 21

Performance of STL vector vs dynamic arrays

I would like to know how big of a difference there is between the performance of c++ dynamic arrays and STL vectors. I know it could totally depend on your code and how you construct your vector, for example not taking advantage of std::vector::reserve and constructing vectors using std::vector::push_back could lead to poor performance. However, I cannot see why someone would choose dynamic arrays (with operator new[]) instead of using vectors with care. Is there a significant performance gap between correctly implemented STL vectors and dynamic arrays in general?

Thanks

Upvotes: 0

Views: 1921

Answers (2)

Caleth
Caleth

Reputation: 62799

There's no performance difference*

std::vector has been carefully specified to be implementable in terms of safely used dynamic arrays, exposing exactly the same features, and more.

[*] Except for expensive to move/copy/default construct types, where std::vector wins over new Expensive[N], as it only allocates space with vector::reserve, the construction of Expensives happens later.

Upvotes: 1

Malcolm McLean
Malcolm McLean

Reputation: 6404

No, stl vectors are a thinnish wrapper over a dynamic array.

Basically new and delete are becoming obsolete in C++, with the exception of the code to implement the stl containers themselves. You should use an std::vector in preference to a hand-managed dynamic array.

Upvotes: 1

Related Questions