Reputation: 83
First, some background. I'm creating a program that contains a sorted list of all lost-and-found items at a theme park, mainly just for schooling purposes. In theory, this program may eventually deal with thousands (or maybe even more) items.
I'm familiar with the clarity/ease-of-use reasons for using a std::vector
rather than an array.
My question: When searching for a specific item in this sorted list, are there any significant performance reasons for why I should go the hard route with an array rather than choosing a std::vector?
NOTE: The vector would not need to be resized. It will be initialized at the start of the program (from a file) and never have items directly added to it.
Upvotes: 1
Views: 1049
Reputation: 81
Go for vector, there are no real compromises in it.
Also, Scott Meyers writer of multiple of the most usefull c++ readings says:
Item 13. Prefer vector and string to dynamically allocated arrays.
Read the whole chapter, you will find lots of reasons why use vectors.
Upvotes: 0
Reputation: 48615
First of all a std::vector
is (virtually) a zero cost abstraction of a dynamic array. Most of the vector's functions are trivial inline functions that get stripped away by the compiler. For that reason a lot of the generated code using a vector is identical to generated code using a dynamic array.
Second the algorithms portion of the Standard Library can't even tell the difference between an array and a vector because vector's iterators compile down to raw pointers, just like an array.
So, I would be very surprised if you could measure any difference given that there most likely won't be any. And if there is, you can always pass the vector's internal array which will, of course, be identical to using an array. Because it is an array!
Upvotes: 2
Reputation: 373
There would be no significant difference between array and vector in terms of sorting speed, no. I'd go with vector as well.
Upvotes: 0