Since the problem is relatively "complex", I wouldn't try to force a solution by using standard algorithms only (since there is no special algorithm to solve your problem. You could probably hack something with remove_if, find and bind2nd or something).
For implementing the algorithm yourself, you have basically two options, with the usual memory vs. speed tradeoff.
The first solution would be to iterate the vector and search and remove duplicates for the current item. This is the cpu-intensive approach.
A maybe faster approach would be creating a second vector (of the same size as the first to minimize memory reallocations) and storing the found items in there. Then, for each iteration of the original vector, only the shorter second vector needs to be searched through to find out whether the current item should be deleted or not.
The first approach works with every iterator, while the second is limited to random access iterators.
Here are the implementations:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template<typename T>
void remove_duplicates_ordered_mem_intensive(T &container)
{
std::vector<typename T::value_type> items;
items.reserve (container.size());
typename T::iterator i = container.begin();
while (i != container.end())
{
if (find (items.begin(), items.end(), *i) != items.end())
i = container.erase(i);
else
{
items.push_back(*i);
++i;
}
}
}
template<typename T>
void remove_duplicates_ordered_slow(T &container)
{
typename T::iterator i = container.begin();
while (i != container.end())
{
typename T::iterator f = i;
++f;
while (f != container.end())
{
if (*f == *i)
f = container.erase(f);
else
++f;
}
++i;
}
}
int main ()
{
vector<int> v;
v.push_back (2);
v.push_back (1);
v.push_back (3);
v.push_back (1);
v.push_back (4);
v.push_back (2);
cout << "Old:\n";
for (vector<int>::const_iterator i = v.begin(); i != v.end(); ++i)
cout << *i << endl;
vector<int> a (v), b (v);
remove_duplicates_ordered_mem_intensive (a);
remove_duplicates_ordered_slow (b);
cout << "\nRemoved duplicates with intensive memory usage:\n";
for (vector<int>::const_iterator i = a.begin(); i != a.end(); ++i)
cout << *i << endl;
cout << "\nRemoved duplicates somewhat slower, without copying:\n";
for (vector<int>::const_iterator i = b.begin(); i != b.end(); ++i)
cout << *i << endl;
}