STL containers are highly efficient and popular, but is there any practical advantage of choosing to use an array instead of an STL container like perhaps, a vector, in any kind of scenario? Arrays have several disadvantages, for example, the size information is lost when passing arrays to a function, since it degrades to a pointer. Do vectors or other STL containers also have some disadvantages in some particular situations?
Asked
Active
Viewed 140 times
0
-
http://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences – NPE Feb 23 '17 at 20:56
-
Also http://stackoverflow.com/questions/3664272/is-stdvector-so-much-slower-than-plain-arrays – NPE Feb 23 '17 at 20:56
-
2Everything is a trade off. The big one with `std::vector` is by default, you are doing dynamic memory allocations. Do note there is now `std::array` to replace raw arrays – NathanOliver Feb 23 '17 at 20:56
-
Also http://stackoverflow.com/questions/6632971/what-is-the-difference-between-stdarray-and-stdvector-when-do-you-use-one-o (even though that's not exactly what you're asking) – NPE Feb 23 '17 at 20:56
-
To clarify, when you say 'Array', do you mean `int arr[3] = {1, 2, 3};`, or `std::array
arr = {1, 2, 3};`? Because in C++, when we say 'array', it's usually better to refer to the latter, and uses to the former are discouraged. – Xirema Feb 23 '17 at 20:57 -
Oh, yeah the link solves my doubt. So should I delete the question as it is a duplicate? – 50calrevolver Feb 23 '17 at 20:58
-
@Xirema given that "array" is presented here in contrast to "STL containers", I assume it's the former. – Quentin Feb 23 '17 at 20:58
-
@50calrevolver No need to delete it, it's been marked as duplicate. – Xirema Feb 23 '17 at 20:59
-
I meant the former one, i.e. int arr[3] = {1, 2, 3}. Actually I am implementing some algorithms for a friend's project and I was thinking that whether I should change all the pre-written code so that vectors are used instead of arrays. Now I am convinced to go with STL – 50calrevolver Feb 23 '17 at 21:00
-
*"the size information is lost when passing arrays to a function"* - Not necessarily. You *can* pass arrays by reference, which then retains the size information. – IInspectable Feb 23 '17 at 21:02