Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::unique_copy,std::ranges::unique_copy_result

      From cppreference.com
      <cpp‎ |algorithm‎ |ranges
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      Numeric operations
      Operations on uninitialized memory
       
      Constrained algorithms
      All names in this menu belong to namespacestd::ranges
      Non-modifying sequence operations
      Modifying sequence operations
      Partitioning operations
      Sorting operations
      Binary search operations (on sorted ranges)
             
             
      Set operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      Permutation operations
      Fold operations
      Operations on uninitialized storage
      Return types
       
      Defined in header<algorithm>
      Call signature
      template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O,

               class Proj=std::identity,
               std::indirect_equivalence_relation<std::projected<I, Proj>>
                    C=ranges::equal_to>
      requiresstd::indirectly_copyable<I, O>&&(std::forward_iterator<I>||
                   (std::input_iterator<O>&&std::same_as<std::iter_value_t<I>,
                       std::iter_value_t<O>>)||std::indirectly_copyable_storable<I, O>)
      constexpr unique_copy_result<I, O>

          unique_copy( I first, S last, O result, C comp={}, Proj proj={});
      (1)(since C++20)
      template<ranges::input_range R,std::weakly_incrementable O,

               class Proj=std::identity,
               std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>,
                    Proj>> C=ranges::equal_to>
      requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>&&
                   (std::forward_iterator<ranges::iterator_t<R>>||
                   (std::input_iterator<O>&&std::same_as<ranges::range_value_t<R>,
                       std::iter_value_t<O>>)||
                   std::indirectly_copyable_storable<ranges::iterator_t<R>, O>)
      constexpr unique_copy_result<ranges::borrowed_iterator_t<R>, O>

          unique_copy( R&& r, O result, C comp={}, Proj proj={});
      (2)(since C++20)
      Helper types
      template<class I,class O>
      using unique_copy_result=ranges::in_out_result<I, O>;
      (3)(since C++20)
      1) Copies the elements from the source range[firstlast), to the destination range beginning atresult in such a way that there are no consecutive equal elements. Only the first element of each group of equal elements is copied.
      The ranges[firstlast) and[resultresult+ N) must not overlap.N=ranges::distance(first, last).
      Two consecutive elements*(i-1) and*i are considered equivalent ifstd::invoke(comp,std::invoke(proj,*(i-1)),std::invoke(proj,*i))==true, wherei is an iterator in the range[first+1last).
      2) Same as(1), but usesr as the range, as if usingranges::begin(r) asfirst, andranges::end(r) aslast.

      The function-like entities described on this page arealgorithm function objects (informally known asniebloids), that is:

      Contents

      [edit]Parameters

      first, last - the iterator-sentinel pair defining the sourcerange of elements to process
      r - the source range of elements
      result - the destination range of elements
      comp - the binary predicate to compare the projected elements
      proj - the projection to apply to the elements

      [edit]Return value

      {last, result+ N}

      [edit]Complexity

      ExactlyN-1 applications of the corresponding predicatecomp and no more than twice as many applications of any projectionproj.

      [edit]Possible implementation

      See also the implementations inlibstdc++ andMSVC STL (and third-party libraries:cmcstl2,NanoRange, andrange-v3).

      struct unique_copy_fn{template<std::input_iterator I,std::sentinel_for<I> S,std::weakly_incrementable O,class Proj=std::identity,std::indirect_equivalence_relation<std::projected<I,                 Proj>> C=ranges::equal_to>    requiresstd::indirectly_copyable<I, O>&&(std::forward_iterator<I>||(std::input_iterator<O>&&std::same_as<std::iter_value_t<I>,std::iter_value_t<O>>)||std::indirectly_copyable_storable<I, O>)constexpr ranges::unique_copy_result<I, O>        operator()(I first, S last, O result, C comp={}, Proj proj={})const{if(!(first== last)){std::iter_value_t<I> value=*first;*result= value;++result;while(!(++first== last)){auto&& value2=*first;if(!std::invoke(comp,std::invoke(proj, value2),std::invoke(proj, value))){                    value=std::forward<decltype(value2)>(value2);*result= value;++result;}}} return{std::move(first), std::move(result)};} template<ranges::input_range R,std::weakly_incrementable O,class Proj=std::identity,std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>,                 Proj>> C=ranges::equal_to>    requiresstd::indirectly_copyable<ranges::iterator_t<R>, O>&&(std::forward_iterator<ranges::iterator_t<R>>||(std::input_iterator<O>&&std::same_as<ranges::range_value_t<R>,std::iter_value_t<O>>)||std::indirectly_copyable_storable<ranges::iterator_t<R>, O>)constexpr ranges::unique_copy_result<ranges::borrowed_iterator_t<R>, O>        operator()(R&& r, O result, C comp={}, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r), std::move(result),                       std::move(comp), std::move(proj));}}; inlineconstexpr unique_copy_fn unique_copy{};

      [edit]Example

      Run this code
      #include <algorithm>#include <cmath>#include <iostream>#include <iterator>#include <list>#include <string>#include <type_traits> void print(constauto& rem,constauto& v){using V=std::remove_cvref_t<decltype(v)>;constexprbool sep{std::is_same_v<typename V::value_type,int>};std::cout<< rem<<std::showpos;for(constauto& e: v)std::cout<< e<<(sep?" ":"");std::cout<<'\n';} int main(){std::string s1{"The      string    with many       spaces!"};    print("s1: ", s1); std::string s2;    std::ranges::unique_copy(        s1.begin(), s1.end(),std::back_inserter(s2),[](char c1,char c2){return c1==' '&& c2==' ';});    print("s2: ", s2); constauto v1={-1,+1,+2,-2,-3,+3,-3};    print("v1: ", v1);std::list<int> v2;    std::ranges::unique_copy(        v1,std::back_inserter(v2),{},// default comparator std::ranges::equal_to[](int x){return std::abs(x);}// projection);    print("v2: ", v2);}

      Output:

      s1: The      string    with many       spaces!s2: The string with many spaces!v1: -1 +1 +2 -2 -3 +3 -3 v2: -1 +2 -3

      [edit]See also

      removes consecutive duplicate elements in a range
      (algorithm function object)[edit]
      copies a range of elements to a new location
      (algorithm function object)[edit]
      finds the first two adjacent items that are equal (or satisfy a given predicate)
      (algorithm function object)[edit]
      creates a copy of some range of elements that contains no consecutive duplicates
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/unique_copy&oldid=180573"

      [8]ページ先頭

      ©2009-2025 Movatter.jp