On Sun, Jan 2, 2022, at 9:40 AM, Ivan Matek via Boost wrote:
Answering multiple > @Robert I think multi_array is still heap allocating, but unlike vector<vector> it does one heap allocation so it is more efficient.
Taking the liberty to appropriate that as a response to my post as well: https://godbolt.org/z/o4x6vGhW5 #include <boost/container/static_vector.hpp> #include <boost/container/small_vector.hpp> #include <boost/multi_array.hpp> #include <fmt/ranges.h> namespace bc = boost::container; int main() { constexpr auto K=2, L=3, M=5, NUM=K*L*M; using boost::extents; using boost::indices; bc::static_vector<float, K*L*M> backing(NUM); // or small_vector that *can* optionally allocate std::iota(std::begin(backing), std::end(backing), 0); boost::multi_array_ref<float, 3> arr(backing.data(), extents[K][L][M]); fmt::print("K×L×M: {}\n", arr); arr.reshape(std::array{L,M,K}); fmt::print("L×M×K: {}\n", arr); using range = boost::multi_array_types::index_range; fmt::print("3d slice: {}\n", arr[indices[range(2, 3)][range(0, M, 2)][range()]]); fmt::print("2d slice: {}\n", arr[indices[range(2, 3)][range(0, M, 2)][1]]); } This demos some of the Multi-Array interface with zero dynamic allocation. The library has more expressive indexing/layout options (see "general storage ordering" https://www.boost.org/doc/libs/1_78_0/libs/multi_array/doc/user.html#sec_sto...).