\\
Copies elements while removing adjacent duplicates
Key Facts
Gyroscopic Couple: The rate of change of angular momentum () = (In the limit).- = Moment of Inertia.
- = Angular velocity
- = Angular velocity of precession.
Blaise Pascal (1623-1662) was a French mathematician, physicist, inventor, writer and Catholic philosopher.
Leonhard Euler (1707-1783) was a pioneering Swiss mathematician and physicist.
Definition
The unique_copy() algorithm is defined in the standard header <algorithm> and in the nonstandard backward-compatibility header <algo.h>.Interface
#include <algorithm> template < class InputIterator, class OutputIterator > OutputIterator unique_copy( InputIterator first, InputIterator last, OutputIterator result ); template < class InputIterator, class OutputIterator, class BinaryPredicate > OutputIterator unique_copy( InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate comp, );Parameters:
Parameter | Description |
---|---|
first | A forward iterator addressing the position of the first element in the source range to be copied |
last | A forward iterator addressing the position one past the final element in the source range to be copied |
result | An output iterator addressing the position of the first element in the destination range that is receiving the copy with consecutive duplicates removed |
comp | User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |
Description
Unique_copy algorithm copies a sequence such as each duplicate consecutive elements become an unique element. The first version usesoperator==
to compare the elements, the second version uses the given binary predicate comp
.Complexity
The complexity is linear; performs(last - first)
applications of operator==
(for the first version) or of comp
(for the second version) and at most last - first
assignments.References
Example:
Example - unique_copy function
Problem
The following program demonstrates how to use unique_copy():
Workings
#include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; bool differenceOne (int elem1, int elem2) { return elem1 + 1 == elem2 || elem1 - 1 == elem2; } int main() { // source data int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7,5, 4, 4 }; int sourceNum = sizeof(source)/sizeof(source[0]); // initialize coll with elements from source list<int> coll; copy(source, source+sourceNum, // source back_inserter(coll)); // destination PRINT_ELEMENTS(coll); // print element with consecutive duplicates removed unique_copy(coll.begin(), coll.end(), // source ostream_iterator<int>(cout," ")); // destination cout <<endl; // print element without consecutive duplicates that differ by one unique_copy(coll.begin(), coll.end(), // source ostream_iterator<int>(cout," "), // destination differenceOne); // duplicate criterion cout <<endl; return 0; }
Solution
Output:
1 4 4 6 1 2 2 3 1 6 6 6 5 7 5 4 4
1 4 6 1 2 3 1 6 5 7 5 4
1 4 4 6 1 3 1 6 6 6 4 4
1 4 6 1 2 3 1 6 5 7 5 4
1 4 4 6 1 3 1 6 6 6 4 4
References
- Nicolai M. Josuttis: "The C++ Standard Library"