Giriş
Açıklaması şöyle
A View is something that you apply on a range and performs some operation. A view does not own data and it's time to copy, move, assignment is constant.
Çoğu view için alternatif bir isim de var. Açıklaması şöyle
In general, you can use a view such as std::views::transform with the alternative name std::transform_view.
Alternatif isimler parametre olarak bir container/view alıyorlar. view olanlar ise "pipe" işareti ile kullanılıyorlar.
Örnek
Şöyle yaparız İlk kullanımda pipe işareti görülebilir. İkinci kullanımda ise parametre olarak pi container değişkeni geçiliyor.
auto print = [](char x) { std::cout << x; };int main() { constexpr char pi[] { '3', '.', '1', '4', '1', '5', '9', '2', '6', '5' }; std::ranges::for_each(pi | std::ranges::views::take(8), print); std::cout << '\n'; std::ranges::for_each(std::ranges::take_view{pi, 8}, print); std::cout << '\n'; }
Çıktı olarak şunu alırız
3.1415923.141592
Örnek
Şöyle yaparız. Burada alternatif isimler kullanıldığı için parametre olarak bir container/view geçiliyor.
const std::string s{"cosmos"};const std::ranges::take_view tv{s, 3};const std::ranges::ref_view rv{tv};std::cout<< std::boolalpha<< "call empty() : " << rv.empty() << '\n'<< "call size() : " << rv.size() << '\n'<< "call begin() : " << *rv.begin() << '\n'<< "call end() : " << *(rv.end()-1) << '\n'<< "call data() : " << rv.data() << '\n'<< "call base() : " << rv.base().size() << '\n' // ~> tv.size()<< "range-for : ";for (const auto c: rv) {std::cout << c;}std::cout << '\n';
Çıktı olarak şunu alırız
call empty() : falsecall size() : 3call begin() : ccall end() : scall data() : cosmoscall base() : 3range-for : cos
std::ranges::views::all - takes all elements
Alternatif : std::ranges::all_view
Örnek ver
std::ranges::views::chunk - C++23 ile geliyor
Örnek
Vector'deki yan yana elemanları pair haline getirmek için şöyle yaparız.
#include <iostream> #include <ranges> #include <vector> int main() { std::vector<int> values = {1,2,3,4,5,6,7,8,9,10}; auto chunk_to_pair = [](auto chunk) { return std::pair(*chunk.begin(), *std::next(chunk.begin())); }; for (auto [first, second] : values | std::ranges::views::chunk(2) | std::ranges::views::transform(chunk_to_pair)) { std::cout << first << second << std::endl; } }
Aynı şeyi std::ranges::views::stride ile şöyle yaparız
#include <iostream> #include <ranges> #include <vector> int main() { std::vector<int> values = {1,2,3,4,5,6,7,8,9,10}; auto odds = values | std::ranges::views::drop(0) | std::ranges::views::stride(2); auto evens = values | std::ranges::views::drop(1) | std::ranges::views::stride(2); for (auto [first, second] : std::ranges::views::zip(odds, evens)) { std::cout << first << second << std::endl; } }
std::ranges::ref_view - takes all elements of another view
Alternatif : Yok
Örnek ver
std::ranges::istream_view - applies operator>> on the view
Alternatif : std::ranges::basic_istream_view
Örnek ver
std::ranges::views::common - converts a view into a std::common_range
Alternatif : std::ranges::common_view
Örnek ver
std::ranges::views::drop - skips the first N elements of another view
Alternatif : std::ranges::drop_view
Örnek ver
std::ranges::views::drop_while - skips the initial elements of another view until the predicate returns false
Alternatif :std::ranges::drop_while_view
Örnek ver
std::ranges::views::elements - creates a view on the N-th element of tuples
Alternatif : std::ranges::elements_view
Örnek ver
std::ranges::views::filter - takes the elements which satisfies the predicate
Alternatif : std::ranges::filter_view
Örnek ver
std::ranges::views::join - joins a view of ranges
Alternatif : std::ranges::join_view
Örnek ver
std::ranges::views::keys - creates a view on the first element of a pair-like values
Alternatif : std::ranges::keys_view
Örnek ver
std::ranges::views::reverse - iterates in reverse order
Alternatif : std::reverse_view
Örnek ver
std::ranges::views::split - splits a view by using a delimiter
Alternatif : std::ranges::split_view
Örnek ver
std::ranges::views::take - takes the first N elements of another view
Alternatif : std::ranges::take_view
Örnek ver
std::ranges::views::take_while - takes the elements of another view as long as the predicate returns true
Alternatif : std::ranges::take_while_view
Örnek ver
std::ranges::views::transform - transforms each element
Alternatif : std::ranges::transform_view
Örnek ver
std::ranges::views::values - creates a view on the second elements of a pair-like values
Alternatif : std::ranges::values_view
Örnek ver
Hiç yorum yok:
Yorum Gönder