Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::span<T,Extent>::operator[]

      From cppreference.com
      <cpp‎ |container‎ |span
       
       
       
       
      constexpr reference operator[]( size_type idx)const;
      (since C++20)

      Returns a reference to theidxth element of the sequence.

      Ifidx< size() isfalse, the behavior is undefined.

      (until C++26)

      Ifidx< size() isfalse:

      • 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

      idx - the index of the element to access

      [edit]Return value

      data()[idx]

      [edit]Exceptions

      Throws nothing.

      [edit]Example

      Run this code
      #include <cstddef>#include <iostream>#include <span>#include <utility> void reverse(std::span<int> span){for(std::size_t i=0, j=std::size(span); i< j;++i){--j;std::swap(span[i], span[j]);}} void print(conststd::span<constint> span){for(int element: span)std::cout<< element<<' ';std::cout<<'\n';} int main(){int data[]{1,2,3,4,5};    print(data);    reverse(data);    print(data);}

      Output:

      1 2 3 4 55 4 3 2 1

      [edit]See also

      (C++26)
      access specified element with bounds checking
      (public member function)[edit]
      direct access to the underlying contiguous storage
      (public member function)[edit]
      returns the number of elements
      (public member function)[edit]
      converts aspan into a view of its underlying bytes
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/span/operator_at&oldid=182279"

      [8]ページ先頭

      ©2009-2025 Movatter.jp