Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::unique_copy

      From cppreference.com
      <cpp‎ |algorithm
       
       
      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
       
      Defined in header<algorithm>
      template<class InputIt,class OutputIt>
      OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first);
      (1)(constexpr since C++20)
      template<class ExecutionPolicy,class ForwardIt1,class ForwardIt2>

      ForwardIt2 unique_copy( ExecutionPolicy&& policy, ForwardIt1 first,

                              ForwardIt1 last, ForwardIt2 d_first);
      (2)(since C++17)
      template<class InputIt,class OutputIt,class BinaryPred>

      OutputIt unique_copy( InputIt first, InputIt last,

                            OutputIt d_first, BinaryPred p);
      (3)(constexpr since C++20)
      template<class ExecutionPolicy,class ForwardIt1,

               class ForwardIt2,class BinaryPred>
      ForwardIt2 unique_copy( ExecutionPolicy&& policy,
                              ForwardIt1 first, ForwardIt1 last,

                              ForwardIt2 d_first, BinaryPred p);
      (4)(since C++17)

      Copies the elements from the range[firstlast), to another range beginning atd_first in such a way that there are no consecutive equal elements. Only the first element of each group of equal elements is copied.

      1) Elements are compared usingoperator==.
      Ifoperator== does not establish anequivalence relation, the behavior is undefined.
      3) Elements are compared using the given binary predicatep.
      Ifp does not establish an equivalence relation, the behavior is undefined.
      2,4) Same as(1,3), but executed according topolicy.
      These overloads participate in overload resolution only if all following conditions are satisfied:

      std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue.

      (until C++20)

      std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue.

      (since C++20)

      If*d_first=*first is invalid(until C++20)*first is notwritable tod_first(since C++20), the program is ill-formed.

      If source and destination ranges overlap, the behavior is undefined.

      GivenT as the value type ofInputIt, if overload(1) or(3) doesnot satisfy all of the following conditions, the behavior is undefined:

      (until C++20)
      (since C++20)

      Contents

      [edit]Parameters

      first, last - the pair of iterators defining the sourcerange of elements to process
      d_first - the beginning of the destination range
      policy - theexecution policy to use
      p - binary predicate which returns ​true if the elements should be treated as equal.

      The signature of the predicate function should be equivalent to the following:

       bool pred(const Type1&a,const Type2&b);

      While the signature does not need to haveconst&, the function must not modify the objects passed to it and must be able to accept all values of type (possibly const)Type1 andType2 regardless ofvalue category (thus,Type1& is not allowed, nor isType1 unless forType1 a move is equivalent to a copy(since C++11)).
      The typesType1 andType2 must be such that an object of typeInputIt can be dereferenced and then implicitly converted to both of them.​

      Type requirements
      -
      InputIt must meet the requirements ofLegacyInputIterator.
      -
      OutputIt must meet the requirements ofLegacyOutputIterator.
      -
      ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator.

      [edit]Return value

      Output iterator to the element past the last written element.

      [edit]Complexity

      Given\(\scriptsize N\)N asstd::distance(first, last):

      1,2) Exactly\(\scriptsize max(0,N-1)\)max(0,N-1) comparisons usingoperator==.
      3,4) Exactly\(\scriptsize max(0,N-1)\)max(0,N-1) applications of the predicatep.

      For overloads(2,4), there may be a performance cost if the value type ofForwardIt1 is not bothCopyConstructible andCopyAssignable.

      [edit]Exceptions

      The overloads with a template parameter namedExecutionPolicy report errors as follows:

      • If execution of a function invoked as part of the algorithm throws an exception andExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.
      • If the algorithm fails to allocate memory,std::bad_alloc is thrown.

      [edit]Possible implementation

      See also the implementations inlibstdc++ andlibc++.

      [edit]Notes

      IfInputIt satisfiesLegacyForwardIterator, this function rereads the input in order to detect duplicates.

      Otherwise, ifOutputIt satisfiesLegacyForwardIterator, and the value type ofInputIt is the same as that ofOutputIt, this function compare*d_first to*first.

      Otherwise, this function compares*first to a local element copy.

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <string> int main(){std::string s1{"A string with mmmany letters!"};std::cout<<"Before: "<< s1<<'\n'; std::string s2;    std::unique_copy(s1.begin(), s1.end(),std::back_inserter(s2),[](char c1,char c2){return c1=='m'&&'m'== c2;}); std::cout<<"After:  "<< s2<<'\n';}

      Output:

      Before: A string with mmmany letters!After:  A string with many letters!

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 239C++98the predicate was appliedstd::distance(first, last) timesapplied one time fewer
      (for non-empty ranges)
      LWG 241C++98the value type ofInputIt was not required to beCopyConstructibleconditionally required
      LWG 538C++98the value type ofInputIt was not required to beCopyAssignableconditionally required
      LWG 2439C++98the value type ofInputIt was not required to be
      CopyConstructible ifOutputIt is aLegacyForwardIterator
      conditionally required

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp