std::ranges...:operator[](3)
β¨ std::ranges::elements_view::iterator::operator[] β¨
okay bestie, welcome to the C++ Standard Libary page for this iteratorβs operator[] β yes, thatβs the manual name, and yes, weβre keeping the spelling exactly as it appears up top. this is the C++20 era, so hydrate and letβs go π
π NAME π
std::ranges::elements_view::iterator::operator[] β std::ranges::elements_view::iterator::operator[]
periodt, thatβs the name and the one-liner both π«Ά
π¦ Synopsis π¦
the signature, verbatim, because we do not mess with signatures:
constexpr decltype(auto) operator[]( difference_type n ) const
(since C++20)
requires ranges::random_access_range<Base>;
it returns an element at specified relative location, as if by /*get-element*/(this->base() + n), where for an expression e, /*get-element*/(e) is:
std::get<N>(*e), ifranges::range_reference_t<Base>is a reference type.- Otherwise,
static_cast<E>(std::get<N>(*e)), whereEisstd::remove_cv_t<std::tuple_element_t<N, ranges::range_reference_t<Base>>>.
πΈ Parameters πΈ
nβ position relative to current location
π Return value π
The element at displacement n relative to the current location. thatβs it, thatβs the tea β
π Example π
// Run this code
#include <iostream>
#include <ranges>
#include <string_view>
#include <tuple>
int main()
{
using T = std::tuple<int, char, std::string_view>;
const auto il =
{
T{1, 'A', "Ξ±"},
T{2, 'B', "Ξ²"},
T{3, 'C', "Ξ³"},
};
std::cout << std::views::elements<0>(il)[1] << ' ' // 2
<< std::views::elements<1>(il)[1] << ' ' // B
<< std::views::elements<2>(il)[1] << '\n'; // Ξ²
}
β Output β
2 B Ξ²
π See also π
operator*β accesses theN^th tuple element (C++20) (public member function)
source/version + date, as promised: http://cppreference.com Β· 2024.06.10 Β· std::ranges...:operator[](3) β mwah, go slay those tuple elements πβ¨
π¦ 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