c++ - Slicing std::array -
is there easy way slice of array in c++?
i.e., i've got
array<double, 10> arr10; and want array consisting of 5 first elements of arr10:
array<double, 5> arr5 = arr10.??? (other populating iterating through first array)
the constructors std::array implicitly defined can't initialize container or range iterators. closest can create helper function takes care of copying during construction. allows single phase initialization believe you're trying achieve.
template<class x, class y> x copyarray(const y& src, const size_t size) { x dst; std::copy(src.begin(), src.begin() + 5, dst.begin()); return dst; } std::array<int, 5> arr5 = copyarray<decltype(arr5)>(arr10, 5); you can use std::copy or iterate through copy yourself.
std::copy(arr10.begin(), arr10.begin() + 5, arr5.begin());
Comments
Post a Comment