Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::get(std::ranges::subrange)

      From cppreference.com
      <cpp‎ |ranges‎ |subrange
       
       
      Ranges library
      Range adaptors
       
       
      Defined in header<ranges>
      template<std::size_t N,class I,class S,ranges::subrange_kind K>

          requires((N==0&&std::copyable<I>)|| N==1)

      constexprauto get(constranges::subrange<I, S, K>& r);
      (1)(since C++20)
      template<std::size_t N,class I,class S,ranges::subrange_kind K>

          requires(N<2)

      constexprauto get(ranges::subrange<I, S, K>&& r);
      (2)(since C++20)
      namespace std{using ranges::get;}
      (3)(since C++20)

      Providesstructured binding support.

      1) Obtains the iterator or sentinel from asubrange lvalue (or a const rvalue) whenN==0 orN==1, respectively.
      2) Same as(1), except that it takes a non-constsubrange rvalue.
      3) Overloads(1,2) are imported into namespacestd, which simplifies their usage and makes everysubrange with a copyable iterator apair-like type.

      Contents

      [edit]Parameters

      r - asubrange

      [edit]Return value

      1,2) IfN is0, returnsr.begin(). Otherwise (N is1), returnsr.end().

      [edit]Example

      Run this code
      #include <array>#include <iostream>#include <iterator>#include <ranges> int main(){std::array a{1,-2,3,-4};     std::ranges::subrange sub_a{std::next(a.begin()),std::prev(a.end())};std::cout<<*std::ranges::get<0>(sub_a)<<' '// == *(begin(a) + 1)<<*std::ranges::get<1>(sub_a)<<'\n';// == *(end(a) - 1) *std::get<0>(sub_a)=42;// OK//  *std::get<2>(sub_a) = 13; // Error: index can only be 0 or 1}

      Output:

      -2 -4

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3589C++20overload(1) copiesbegin_ ifN is0, butI might not modelcopyableadded constraints

      [edit]See also

      Structured binding(C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
      tuple accesses specified element
      (function template)[edit]
      accesses an element of apair
      (function template)[edit]
      accesses an element of anarray
      (function template)[edit]
      reads the value of the variant given the index or the type (if the type is unique), throws on error
      (function template)[edit]
      obtains a reference to real or imaginary part from astd::complex
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/ranges/subrange/get&oldid=176929"

      [8]ページ先頭

      ©2009-2025 Movatter.jp