Skip to content

Instantly share code, notes, and snippets.

@nurettin
Created August 3, 2013 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurettin/6145753 to your computer and use it in GitHub Desktop.
Save nurettin/6145753 to your computer and use it in GitHub Desktop.
counting unique
#include <cstdlib>
namespace pwned {
template <class ForwardIterator, class OutputIterator>
ForwardIterator counting_unique (ForwardIterator first, ForwardIterator last, OutputIterator out)
{
if (first==last) return last;
ForwardIterator result = first;
std::size_t count= 1;
while (++first != last)
{
if (!(*result == *first))
{
*(++result)=*first;
*out= count;
++ out;
count= 1;
}
else
{
++ count;
}
}
*out= count;
++ out;
return ++result;
}
} // pwned
#include <iostream>
#include <iterator>
int main()
{
int a[]= { 1, 1, 2, 3, 3, 4, 4, 5, 4, 4, 4 };
std::ostream_iterator<int> out(std::cout);
int* e= pwned::counting_unique(a, a+ 11, out);
std::cout<< std::endl;
std::copy(a, e, out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment