This C++ program demonstrates the unique_copy() algorithm. The function unique_copy() takes three iterators as parameters – iterators to the beginning and end of the container and iterator to the container or output stream where the unique elements would be copied. The program demonstrates the use of the algorithm which removes the adjacent duplicate characters in the container but doesn’t modify the container elements.
Here is the source code of the C++ program which demonstrates the unique_copy() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to remove equal adjacent elements using unique_copy() algorithm
*/
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <iterator>
using namespace std;
void print(char a[], int N)
{
for(int i = 0; i < N; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
char a[] = {'a', 'a', 'a', 'e', 'c', 'd', 'd', 'e', 'e', 'e'};
int alen = sizeof(a) / sizeof(char);
cout << "Characters : ";
print(a, alen);
cout << "Removing duplicate adjacent characters ... " << endl;
cout << "Characters : ";
std::unique_copy(a, a + alen, ostream_iterator<char>(cout, " "));
cout << endl;
}
$ a.out Characters : a a a e c d d e e e Removing duplicate adjacent characters ... Characters : a e c d e
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check Computer Science Books
- Apply for C++ Internship
- Check C++ Books