std::ranges...e_view::size(3)
πΈ NAME πΈ
hey bestie, welcome to the C++ Standard Libary manual (yes, the header spells it that way, iykyk), section (3) β this page is std::ranges::slide_view::size, and the NAME line is literally:
std::ranges::slide_view::size - std::ranges::slide_view::size
periodt. π
π¦ Synopsis π¦
okay so there are two size() overloads, both since C++23:
constexpr auto size() (since C++23)
requires ranges::sized_range<V>;
constexpr auto size() const (since C++23)
requires ranges::sized_range<const V>;
Returns the number of elements. β¨
Let base_ and n_ be the underlying view and "window size" respectively. (Exact names, no cap.)
Equivalent to:
auto sz = ranges::distance(base_) - n_ + 1;
if (sz < 0)
sz = 0;
return __to_unsigned_like(sz);
π Parameters π
(none) β literally no parameters, bestie. Zero. Nada. π«Ά
π Return value π
The number of elements. Equals to 0, if the number of elements (ranges::size(base_)) in underlying view base_ is less than "window size" n_. That's the whole vibe. π
π§ Example π§
// Run this code β go off:
#include <forward_list>
#include <iostream>
#include <list>
#include <ranges>
int main()
{
constexpr static auto v = {1, 2, 3, 4, 5, 6};
constexpr int width1{4};
constexpr auto view1{std::views::slide(v, width1)};
static_assert(view1.size() == 3);
static_assert(view1.size() == (v.size() - width1 + 1));
constexpr int width2{8};
constexpr auto view2{std::views::slide(v, width2)};
// window is too wide, so view2 has no elements:
static_assert(view2.size() == 0);
std::forward_list forward_list = v;
const auto view3{std::views::slide(forward_list, width1)};
// auto x = view3.size(); // error: sized_range constraint is not satisfied
std::list list = v;
const auto view4{std::views::slide(list, width1)};
std::cout << view4.size() << '\n'; // prints 3
}
β Output β
3
π See also π
ranges::sizeβ returns an integer equal to the size of a range (C++20) (customization point object)ranges::ssizeβ returns a signed integer equal to the size of a range (C++20) (customization point object)
mwah π β source/version: http://cppreference.com, dated 2024.06.10. slay and read the docs, bestie! ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms