Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::move_iterator

      From cppreference.com
      <cpp‎ |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)
       
       
      Defined in header<iterator>
      template<class Iter>
      class move_iterator;
      (since C++11)

      std::move_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least aLegacyInputIterator or modelinput_iterator(since C++20), or stronger iterator concept(since C++23)), except that dereferencing converts the value returned by the underlying iterator into an rvalue. If this iterator is used as an input iterator, the effect is that the values are moved from, rather than copied from.

      Contents

      [edit]Nested types

      Type Definition
      iterator_typeIter
      iterator_categorystd::iterator_traits<Iter>::iterator_category
      value_typestd::iterator_traits<Iter>::value_type
      difference_typestd::iterator_traits<Iter>::difference_type
      pointerIter
      reference
      (until C++20)
      Type Definition
      iterator_typeIter
      iterator_category
      (conditionally present)
      iterator_concept

      std::input_iterator_tag

      (until C++23)
      (since C++23)
      value_typestd::iter_value_t<Iter>
      difference_typestd::iter_difference_t<Iter>
      pointerIter
      referencestd::iter_rvalue_reference_t<Iter>
      (since C++20)

      [edit]Data members

      Member Description
      Itercurrent the underlying iterator
      (exposition-only member object*)

      [edit]Member functions

      constructs a newmove_iterator
      (public member function)[edit]
      assigns anothermove_iterator
      (public member function)[edit]
      accesses the underlying iterator
      (public member function)[edit]
      accesses the pointed-to element
      (public member function)[edit]
      accesses an element by index
      (public member function)[edit]
      advances or decrements themove_iterator
      (public member function)[edit]

      [edit]Non-member functions

      (C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
      compares the underlying iterators
      (function template)[edit]
      compares the underlying iterator and the underlying sentinel
      (function template)[edit]
      (C++11)
      advances the iterator
      (function template)[edit]
      (C++11)
      computes the distance between two iterator adaptors
      (function template)[edit]
      computes the distance between the underlying iterator and the underlying sentinel
      (function template)[edit]
      (C++20)
      casts the result of dereferencing the underlying iterator to its associated rvalue reference type
      (function)[edit]
      (C++20)
      swaps the objects pointed to by two underlying iterators
      (function template)[edit]
      creates astd::move_iterator of type inferred from the argument
      (function template)[edit]

      [edit]Helper templates

      template<class Iterator1,class Iterator2>

          requires(!std::sized_sentinel_for<Iterator1, Iterator2>)
      constexprbool disable_sized_sentinel_for

         <std::move_iterator<Iterator1>, std::move_iterator<Iterator2>>=true;
      (since C++20)

      This partial specialization ofstd::disable_sized_sentinel_for prevents specializations ofmove_iterator from satisfyingsized_sentinel_for if their underlying iterators do not satisfy the concept.

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_move_iterator_concept202207L(C++23)Makestd::move_iterator<T*> a random access iterator

      [edit]Example

      Run this code
      #include <algorithm>#include <iomanip>#include <iostream>#include <iterator>#include <ranges>#include <string>#include <string_view>#include <vector> void print(conststd::string_view rem,constauto& v){std::cout<< rem;for(constauto& s: v)std::cout<<std::quoted(s)<<' ';std::cout<<'\n';}; int main(){std::vector<std::string> v{"this","_","is","_","an","_","example"};    print("Old contents of the vector: ", v);std::string concat;for(auto begin=std::make_move_iterator(v.begin()),              end=std::make_move_iterator(v.end());         begin!= end;++begin){std::string temp{*begin};// moves the contents of *begin to temp        concat+= temp;} // Starting from C++17, which introduced class template argument deduction,// the constructor of std::move_iterator can be used directly:// std::string concat = std::accumulate(std::move_iterator(v.begin()),//                                      std::move_iterator(v.end()),//                                      std::string());     print("New contents of the vector: ", v);    print("Concatenated as string: ", std::ranges::single_view(concat));}

      Possible output:

      Old contents of the vector: "this" "_" "is" "_" "an" "_" "example"New contents of the vector: "" "" "" "" "" "" ""Concatenated as string: "this_is_an_example"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2106C++11dereferencing amove_iterator could return a dangling reference
      if the dereferencing the underlying iterator returns a prvalue
      returns the
      object instead
      LWG 3736C++20move_iterator was missingdisable_sized_sentinel_for specializationadded
      P2259R1C++20memberiterator_category was defined even if
      std::iterator_traits<Iter>::iterator_category is not defined
      iterator_category is
      not defined in this case

      [edit]See also

      creates astd::move_iterator of type inferred from the argument
      (function template)[edit]
      sentinel adaptor forstd::move_iterator
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/iterator/move_iterator&oldid=177327"

      [8]ページ先頭

      ©2009-2025 Movatter.jp