/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016-2020 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** range.cxx : C++20 ranges **** **** Inspired by https://www.youtube.com/watch?v=d9ToM7sIvq0&t=5s **** ****************************************************************/ #include using std::cout; #include using std::stringstream; #include using std::string; #include using std::transform; #include using std::vector; //#include // views::sliding & to<> not yet in gcc12 #include #ifdef RANGES_V3_ALL_HPP namespace rng = ranges; #else namespace rng = std::ranges; #endif template< typename T > string vector_as_string( const vector &v ) { stringstream s; for ( auto e : v ) s << e << ", "; return s.str(); }; int main() { { vector v{ 1,2,3,4,5,6 }; auto sum = [] ( rng::subrange::iterator> subrang ) { auto [ first,tail ] = subrang; float x = *( subrang.begin() ), y = *( subrang.begin()+1 ), z = *( subrang.begin()+2 ); return -x +2*y -z; }; auto w = v | rng::views::sliding(3) | rng::views::transform(sum); // | rng::to>; // cout << vector_as_string(w) << '\n'; } return 0; }