What is the best replacement for std::array<...>
if I don't want to have to provide constexpr size? I figured it would be best to just use std::vector
and do reserve(...)
on it, but maybe I'm overlooking something?
std::vector is the correct replacement for most cases, QVector is preferable for code that uses Qt, while std::array can be used in those cases where granular memory control is critical.
In C++, we use sizeof() operator to find the size of desired data type, variables, and constants. It is a compile-time execution operator. We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
std::vector
should be the correct container of choice, if the size needs to be determined at runtime.
Yes, use std::vector
.
So if your code is
std:array<int, 42> my_array;
Replace it by
std:vector<int> my_array(42);
Note: you probably don't want to use reserve
, because it leaves the vector
empty. If you are using std::array
, your code doesn't have the concept of empty array, so it's best represented by a std::vector
instance that is filled at construction, and never resized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With