Slicing using standard library

Is there functionality in the standard library that allows me to slice std::slice or do I need to write something like

 std::slice refine(std::slice const& first, std::slice const& second) { return { first.start() + second.start() * first.stride(), second.size(), first.stride() * second.stride() }; } 

on your own?

+7
c ++ slice c ++ 14 valarray
source share
1 answer

There is no such thing in the standard library as far as I can tell.

http://en.cppreference.com/w/cpp/numeric/valarray/slice

http://en.cppreference.com/w/cpp/numeric/valarray/slice_array

But if you use const std :: valarray, you can use the operator

 valarray<T> operator[] (slice slc) const; 

which will return another std :: valarray, in which case you can apply std :: slice again.

 // slice example #include <iostream> // std::cout #include <cstddef> // std::size_t #include <valarray> // std::valarray, std::slice int main() { std::valarray<int> foo(100); for (int i = 0; i < 100; ++i) foo[i] = i; std::valarray<int> bar = foo[std::slice(2, 20, 4)]; std::cout << "slice(2,20,4):"; for (auto b : bar) std::cout << ' ' << b; std::cout << '\n'; auto const cfoo = foo; std::valarray<int> baz = cfoo[std::slice(2, 20, 4)][std::slice(3, 3, 3)]; std::cout << "slice(3,3,3):"; for (auto b : baz) std::cout << ' ' << b; std::cout << '\n'; return 0; } 
0
source share

All Articles