Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      operator-(std::counted_iterator)

      From cppreference.com
      <cpp‎ |iterator‎ |counted iterator
       
       
      Iterator library
      Iterator concepts
      Iterator primitives
      Algorithm concepts and utilities
      Indirect callable concepts
      Common algorithm requirements
      (C++20)
      (C++20)
      (C++20)
      Utilities
      (C++20)
      Iterator adaptors
      Range access
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++11)(C++14)
      (C++14)(C++14)  
      (C++17)(C++20)
      (C++17)
      (C++17)
       
       
      template<std::common_with<I> I2>

         friendconstexprstd::iter_difference_t<I2> operator-(

             const counted_iterator& x,const counted_iterator<I2>& y);
      (since C++20)

      Computes the distance between two iterator adaptors.

      The behavior is undefined ifx andy do not point to elements of the same sequence. That is, there must exist somen such thatstd::next(x.base(), x.count()+ n) andstd::next(y.base(), y.count()+ n) refer to the same element.

      This function template is not visible to ordinaryunqualified orqualified lookup, and can only be found byargument-dependent lookup when std::counted_iterator<I> is an associated class of the arguments.

      Contents

      [edit]Parameters

      x, y - iterator adaptors to compute the difference of

      [edit]Return value

      y.count()- x.count()

      [edit]Notes

      Since thelength counts down, not up, the order of the arguments ofoperator- in the underlying expression is reversed, i.e.y islhs andx isrhs.

      [edit]Example

      Run this code
      #include <initializer_list>#include <iterator> int main(){staticconstexprauto v={1,2,3,4,5,6};constexprstd::counted_iterator<std::initializer_list<int>::iterator>        it1{v.begin(),5},        it2{it1+3},        it3{v.begin(),2};     static_assert(it1- it2==-3);    static_assert(it2- it1==+3);//  static_assert(it1 - it3 == -3); // UB: operands of operator- do not refer to// elements of the same sequence}

      [edit]See also

      advances or decrements thecounted_iterator
      (public member function)[edit]
      (C++20)
      advances the iterator
      (function template)[edit]
      computes the signed distance to the end
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/counted_iterator/operator-&oldid=159835"

      [8]ページ先頭

      ©2009-2025 Movatter.jp