This C++ program removes equal adjacent elements using unique() algorithm. The algorithm removes duplicate characters which are adjacent to each other in a container. The program creates an array of characters and runs the algorithm on the array. The array elements are then printed on the standard output.
Here is the source code of the C++ program which removes equal adjacent elements using unique() 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() 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);
char b[alen];
char * p;
cout << "Characters : ";
print(a, alen);
cout << "Removing duplicate adjacent characters ... " << endl;
cout << "Characters : ";
// unique returns pointer to one element ahead of last element
p = unique(a, a + alen);
print(a, p - a);
}
$ 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.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Check Computer Science Books
- Check Programming Books
- Apply for C++ Internship
- Check C++ Books