C++ ranges::view::VIEW, ALL, COUNTED Introductiion














































C++ ranges::view::VIEW, ALL, COUNTED Introductiion



In this series of articles on RANGES with C++, today we are going to see libraries of std::ranges::View, std::ranges::view::all, std::ranges::view::counted
____________________________________________________________________________________

INTRODUCTION to RANGES HAS BEEN DONE IN MY FIRST ARTICLE OF RANGES LIBRARY

All the libraries defined below are included in the header <ranges> and <iterator>.

Here we are going to discuss about views and its libraries.


std::ranges::View


View is included in the <ranges> header.


As already defined in my previous articles, it specifies the requirements of a Range type that has constant time copy, move, assignment operations (e.g. a pair of iterators that creates its elements on-demand).


std::ranges::view::all


It is a range adaptor that returns a View that includes all elements of its Range argument.


Sample Code


#include <ranges>
#include <vector>
#include <iostream>

int main()
{ int n;
std
::vector<int> ve{12,13,5,2,4};
for(n : std::view::all(ve) | std::view::take(3) ) {
std
::cout << n;
}
}


OUTPUT


12 13  5


std::ranges::view::counted


 It presents a View of elements of the counted range [a,n) for some iterator a and non-negative integer n,as shown in   the example below.


Sample Code


#include <ranges>
#include <iostream>

int main()
{
int
array[] = {3,5,7,1,87,3,7,2,6,3};
for(int i : std::view::counted(array, 5))
std::cout << i << ' ';
}

Output


3 5 7 1 87


Comments