Advantages Of Using Arrays Instead Of Std::vector?
Answer :
In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays:
- Arrays are slightly more compact: the size is implicit.
- Arrays are non-resizable; sometimes this is desirable.
- Arrays don't require parsing extra STL headers (compile time).
- It can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using).
- Fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed.
Because C++03 has no vector literals. Using arrays can sometime produce more succinct code.
Compared to array initialization:
char arr[4] = {'A', 'B', 'C', 'D'};
vector initialization can look somewhat verbose
std::vector<char> v; v.push_back('A'); v.push_back('B'); ...
I'd go for std::array available in C++0x instead of plain arrays which can also be initialized like standard arrays with an initializer list
https://en.cppreference.com/w/cpp/container/array
Comments
Post a Comment