You should very carefully consider whether or not you really don't want
to use std::unique(). Here are the unsorted_unique() and related
algorithms from my STL Extensions library, and it does what I believe
you want done. But it is extremely expensive. Choose your tools
wisely.
/* ---
25.2.8
template< class ForwardIterator, class OutputIterator, class
BinaryPredicate>
OutputIterator unsorted_unique_copy(ForwardIterator first,
InputIterator last, OutputIterator result, BinaryPredicate pred)
template< class ForwardIterator, class OutputIterator>
OutputIterator unsorted_unique_copy(ForwardIterator first,
InputIterator last, OutputIterator result)
template< class ForwardIterator, class OutputIterator, class
BinaryPredicate>
ForwardIterator unsorted_unique(ForwardIterator first,
ForwardIterator last, OutputIterator result, BinaryPredicate pred)
template< class ForwardIterator, class OutputIterator>
ForwardIterator unsorted_unique(ForwardIterator first,
ForwardIterator last, OutputIterator result)
Requires :
The ranges [first, last) and [result, result+last-first) shall not
overlap
Effects :
Eliminates all but the first occurance of any element referred to by
the iterator i in the range [first, last).
The range [first, last) does not need to be sorted before executing
unsorted_unique_copy.
Complexity :
If the range [first, last) is not empty, (n^2 + n) / 2 applications
of the comparison predicate or operator.
Otherwise, no applications of the predicate.
Returns :
The end of the resulting range.
--- */
template< class ForwardIterator, class OutputIterator, class
BinaryPredicate>
OutputIterator unsorted_unique_copy(ForwardIterator first,
ForwardIterator last, OutputIterator result, BinaryPredicate pred)
{
for( ForwardIterator current = first; current != last; ++current )
{
// make sure this item isnt repeated previously in the source range
if( current == find_if(first, current, bind1st(pred,*current)) )
// wasn't found, add to destination range
*result++ = *current;
}
return result;
}
template< class ForwardIterator, class OutputIterator>
OutputIterator unsorted_unique_copy(ForwardIterator first,
ForwardIterator last, OutputIterator result)
{
for( ForwardIterator current = first; current != last; ++current )
{
// make sure this item isnt repeated previously in the source range
if( current == find(first, current, *current) )
// wasn't found, add to destination range
*result++ = *current;
}
return result;
}
template< class BidirectionalIterator> inline
BidirectionalIterator unsorted_unique(BidirectionalIterator first,
BidirectionalIterator last)
{
for( ; first != last; ++first )
{
BidirectionalIterator current = first;
for( ++current; current != last; )
{
if( *first == *current )
{
BidirectionalIterator rotend(current);
++rotend;
rotate(current,rotend,last--);
}
else
++current;
}
}
return last;
}
template< class BidirectionalIterator, class BinaryPredicate>
BidirectionalIterator unsorted_unique(BidirectionalIterator first,
BidirectionalIterator last, BinaryPredicate pred)
{
for( ; first != last; ++first )
{
BidirectionalIterator current(first);
for( current++; current != last; )
{
if( pred(*first, *current) )
{
BidirectionalIterator rotend(current);
rotend++;
rotate(current,rotend,last--);
}
else
++current;
}
}
return last;
}
</dib>