Or login with:
#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 |
operator== to compare the elements, the second version uses the given binary predicate comp.(last - first) applications of operator== (for the first version) or of comp (for the second version) and at most last - first assignments.#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; }
You must login to leave a messge