I am writing string sorting and I couldn’t decide whether I should use array or vector so that swap operation which I used in my algorithm is faster.
Assume I have a vector and array like that.
vector<string> vec;
string str[20];
Which one of the following swap operations would be faster, or they are equivalent?
vector[i].swap(vector[j]);
str[i].swap(str[j]);
They are equivalent. In fact, not only are they equivalent, they are identical, because in each case you are calling the exact same function:
But, on to the larger question — should you use an array or should you use a
vector. As a general rule of thumb, which I think I’d be hard pressed to find many legitimate exceptions to — you should always use avectorby default in C++ unless you have a specific reason not to. Speed will not be one of those reasons except in extraordinarily rare circumstances.