Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::ranges::unique

      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::permutable I,std::sentinel_for<I> S,class Proj=std::identity,

               std::indirect_equivalence_relation<std::projected<I, Proj>>
                    C=ranges::equal_to>
      constexprranges::subrange<I>

          unique( I first, S last, C comp={}, Proj proj={});
      (1)(since C++20)
      template<ranges::forward_range R,class Proj=std::identity,

               std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>>
                    C=ranges::equal_to>
      requiresstd::permutable<ranges::iterator_t<R>>
      constexprranges::borrowed_subrange_t<R>

          unique( R&& r, C comp={}, Proj proj={});
      (2)(since C++20)
      1) Eliminates all except the first element from every consecutive group of equivalent elements from the range[firstlast) and returns a subrange[retlast), whereret is a past-the-end iterator for the new end of the range.
      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 therange of elements to process
      r - the range of elements to process
      comp - the binary predicate to compare the projected elements
      proj - the projection to apply to the elements

      [edit]Return value

      Returns{ret, last}, whereret is a past-the-end iterator for the new end of the range.

      [edit]Complexity

      For nonempty ranges, exactlyranges::distance(first, last)-1 applications of the corresponding predicatecomp and no more that twice as many applications of any projectionproj.

      [edit]Notes

      Removing is done by shifting (by means of move assignment) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range. Relative order of the elements that remain is preserved and thephysical size of the container is unchanged. Iterators in[retlast) (if any) are still dereferenceable, but the elements themselves have unspecified values (as perMoveAssignable post-condition).

      A call toranges::unique is sometimes followed by a call to a container’serase member function, which erases the unspecified values and reduces thephysical size of the container to match its newlogical size. These two invocations together model theErase–remove idiom.

      [edit]Possible implementation

      struct unique_fn{template<std::permutable I,std::sentinel_for<I> S,class Proj=std::identity,std::indirect_equivalence_relation<std::projected<I, Proj>>                 C=ranges::equal_to>constexprranges::subrange<I>        operator()(I first, S last, C comp={}, Proj proj={})const{        first=ranges::adjacent_find(first, last, comp, proj);if(first== last)return{first, first};auto i{first};++first;while(++first!= last)if(!std::invoke(comp,std::invoke(proj,*i),std::invoke(proj,*first)))*++i=ranges::iter_move(first);return{++i, first};} template<ranges::forward_range R,class Proj=std::identity,std::indirect_equivalence_relation<std::projected<ranges::iterator_t<R>, Proj>>                 C=ranges::equal_to>    requiresstd::permutable<ranges::iterator_t<R>>constexprranges::borrowed_subrange_t<R>        operator()(R&& r, C comp={}, Proj proj={})const{return(*this)(ranges::begin(r),ranges::end(r),                       std::move(comp), std::move(proj));}}; inlineconstexpr unique_fn unique{};

      [edit]Example

      Run this code
      #include <algorithm>#include <cmath>#include <complex>#include <iostream>#include <vector> struct id{int i;explicit id(int i): i{i}{}}; void print(id i,constauto& v){std::cout<< i.i<<") ";    std::ranges::for_each(v,[](autoconst& e){std::cout<< e<<' ';});std::cout<<'\n';} int main(){// a vector containing several duplicated elementsstd::vector<int> v{1,2,1,1,3,3,3,4,5,4};     print(id{1}, v); // remove consecutive (adjacent) duplicatesconstauto ret= std::ranges::unique(v);// v now holds {1 2 1 3 4 5 4 x x x}, where 'x' is indeterminate    v.erase(ret.begin(), ret.end());    print(id{2}, v); // sort followed by unique, to remove all duplicates    std::ranges::sort(v);// {1 1 2 3 4 4 5}    print(id{3}, v); constauto[first, last]= std::ranges::unique(v.begin(), v.end());// v now holds {1 2 3 4 5 x x}, where 'x' is indeterminate    v.erase(first, last);    print(id{4}, v); // unique with custom comparison and projectionstd::vector<std::complex<int>> vc{{1,1},{-1,2},{-2,3},{2,4},{-3,5}};    print(id{5}, vc); constauto ret2= std::ranges::unique(vc,// consider two complex nums equal if their real parts are equal by module:[](int x,int y){return std::abs(x)== std::abs(y);},// comp[](std::complex<int> z){return z.real();}// proj);    vc.erase(ret2.begin(), ret2.end());    print(id{6}, vc);}

      Output:

      1) 1 2 1 1 3 3 3 4 5 42) 1 2 1 3 4 5 43) 1 1 2 3 4 4 54) 1 2 3 4 55) (1,1) (-1,2) (-2,3) (2,4) (-3,5)6) (1,1) (-2,3) (-3,5)

      [edit]See also

      creates a copy of some range of elements that contains no consecutive duplicates
      (algorithm function object)[edit]
      finds the first two adjacent items that are equal (or satisfy a given predicate)
      (algorithm function object)[edit]
      removes elements satisfying specific criteria
      (algorithm function object)[edit]
      removes consecutive duplicate elements in a range
      (function template)[edit]
      removes consecutive duplicate elements
      (public member function ofstd::list<T,Allocator>)[edit]
      removes consecutive duplicate elements
      (public member function ofstd::forward_list<T,Allocator>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/ranges/unique&oldid=180509"

      [8]ページ先頭

      ©2009-2025 Movatter.jp