Created
August 3, 2013 08:44
-
-
Save nurettin/6145753 to your computer and use it in GitHub Desktop.
counting unique
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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