Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::span<T,Extent>::last

      From cppreference.com
      <cpp‎ |container‎ |span
       
       
       
       
      template<std::size_t Count>
      constexprstd::span<element_type, Count> last()const;
      (1)(since C++20)
      constexprstd::span<element_type,std::dynamic_extent>
          last( size_type count)const;
      (2)(since C++20)

      Obtains a subview over the lastCount orcount elements of this span.

      1) The element count is provided as a template argument, and the subview has a static extent.
      IfCount> Extent istrue, the program is ill-formed.
      2) The element count is provided as a function argument, and the subview has a dynamic extent.

      IfCount> size() orcount> size() istrue, the behavior is undefined.

      (until C++26)

      IfCount> size() orcount> size() istrue:

      • If the implementation ishardened, acontract violation occurs. Moreover, if the contract-violation handler returns under “observe” evaluation semantic, the behavior is undefined.
      • If the implementation is not hardened, the behavior is undefined.
      (since C++26)

      Contents

      [edit]Parameters

      count - the number of the elements of the subview

      [edit]Return value

      1)std::span<element_type, Count>{data()+(size()- Count), Count}
      2)std::span<element_type,std::dynamic_extent>{data()+(size()- count), count}

      [edit]Example

      Run this code
      #include <iostream>#include <span>#include <string_view> void println(conststd::string_view title,constauto& container){std::cout<< title<<'['<<std::size(container)<<"]{ ";for(constauto& elem: container)std::cout<< elem<<", ";std::cout<<"};\n";}; void run(std::span<constint> span){    println("span: ", span); std::span<constint,3> span_last= span.last<3>();    println("span.last<3>(): ", span_last); std::span<constint,std::dynamic_extent> span_last_dynamic= span.last(2);    println("span.last(2): ", span_last_dynamic);} int main(){int a[8]{1,2,3,4,5,6,7,8};    println("int a", a);    run(a);}

      Output:

      int a[8]{ 1, 2, 3, 4, 5, 6, 7, 8, };span: [8]{ 1, 2, 3, 4, 5, 6, 7, 8, };span.last<3>(): [3]{ 6, 7, 8, };span.last(2): [2]{ 7, 8, };

      [edit]See also

      obtains a subspan consisting of the firstN elements of the sequence
      (public member function)[edit]
      obtains a subspan
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/span/last&oldid=182292"

      [8]ページ先頭

      ©2009-2025 Movatter.jp