C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm unique_copy()C++ Algorithm unique_copy() function is used to copy a sequence such as each duplicate consecutive element becomes a unique element.
Syntaxequality (1) template <class InputIterator, class OutputIterator> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result); predicate (2) template <class InputIterator, class OutputIterator, class BinaryPredicate> OutputIterator unique_copy (InputIterator first, InputIterator last, OutputIterator result, BinaryPredicate pred); Parameterfirst: A forward iterator pointing the position of the first element in the range to be copied. last: A forward iterator pointing the position one past the final element in the range to be copied. pred: A user-defined predicate function object that defines the condition to be satisfied if two elements in a range are to be taken as equivalent. A binary predicate returns two arguments and returns true when satisfied and false when not satisfied. result: An output iterator pointing the position of the first element in the copied range that is receiving the copy with consecutive duplicates removed. Return valueAn iterator pointing to the new end of the copied range[first, last) that contains no consecutive duplicates. ComplexityComplexity is linear in the range [first, last): compares each pairs of consecutive elements, and performs assignment operation on some of them. Data racesThe object in the range [first, last) are accessed and the objects in the range between result and the returned value are modified. Exception safetyThis function throws an exception if any of pred, the element comparisons, the element assignments or the operations on iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of unique_copy() where elements will be compared by operator==: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 100, 100, 300, 300, 300, 500, 100, 300, 300, 600, 600, 700 }; // vector to store the copied value vector<int> v1(10); vector<int>::iterator ip; // Using unique_copy ip = unique_copy(v.begin(), v.begin() + 12, v1.begin()); // Resizing vector v1 v1.resize(distance(v1.begin(), ip)); cout << "Before: "; for (ip = v.begin(); ip != v.end(); ++ip) { cout << *ip << " "; } // Displaying vector after applying unique_copy cout << "\n\nAfter: "; for (ip = v1.begin(); ip != v1.end(); ++ip) { cout << *ip << " "; } return 0; } Output: Before: 100 100 300 300 300 500 100 300 300 600 600 700 After: 100 300 500 100 300 600 700 In the above example, all the sub-group of consecutive duplicate elements from vector v have been reduced to only one element and copied to new vector v1. Example 2Let's see another simple example to illustrate the use of unique_copy() where elements will be compared by pre-defined function: #include <iostream> #include <algorithm> #include <string> using namespace std; // declaring a BinaryFunction bool Pred(char a, char b) { // It checks if the both the arguments are same and equal // to 'v' then only they are considered same and duplicates are removed if (a == b && a == 'v') { return 1; } else { return 0; } } int main() { // Declaring a string string s = "You arre vvvisiting vvvogie bbogie", s1; // Using std::unique_copy to remove the consecutive // v in the word and copy it to s1 auto ip = unique_copy(s.begin(), s.end(), back_inserter(s1), Pred); cout << "Before: " << s; // Displaying the corrected string cout << "\nAfter: " << s1; return 0; } Output: Before: You arre vvvisiting vvvogie bbogie After: You arre visiting vogie bbogie Example 3Let's see another simple example to check whether the container contains duplicates elements or not: #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 1, 4, 3, 5, 2, 7, 6 }; vector<int>::iterator ip; // Sorting the array to make duplicate elements // consecutive sort(v.begin(), v.end()); // Now v becomes 1 2 3 4 5 6 7 // Declaring a container to store the unique elements vector<int> v1(7); // Using unique_copy ip = unique_copy(v.begin(), v.end(), v1.begin()); // Now v1 becomes {1 2 3 4 5 6 7 } if (v == v1) { cout << "v1 contains only unique elements"; } else { cout << "v1 contains duplicate elements"; } return 0; } Output: v1 contains only unique elements In the above example, first we remove the duplicate elements from vector v and store the resultant elements into another vector v1 and then compare the v1 to v, if both are the same. (Means previous vector v contains only unique elements and there is no duplicate element). Example 4Let's see another simple example to remove all the spaces from the given statement: #include <string> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { string s1 = "The string with many spaces!"; cout << "before: " << s1 << '\n'; string s2; unique_copy(s1.begin(), s1.end(), std::back_inserter(s2), [](char c1, char c2){ return c1 == ' ' && c2 == ' '; }); cout << "after: " << s2 << '\n'; } Output: before: The string with many spaces! after: The string with many spaces!
Next TopicC++ Algorithm
|