I would like to eliminate duplicate elements from a vector while keeping the vector in the current order.
Below I have a proposed implementation. First, is this safe?
Second, is there a better way to do it, either more efficiently or better from a "using C++ algorithms and not reinventing the wheel" perspective.
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
int main()
{
using namespace std;
std::vector<int> v= {1, 7, 2, 3, 8, 4, 5, 3, 2, 3, 2, 6, 2, 3, 2, 9, 10, 1, 2, 2, 1};
std::vector<int>::iterator finalEnd = v.end();
for (auto vIter = v.begin(); vIter != v.end(); ++vIter) {
for (auto nextvIter = vIter + 1; nextvIter != v.end(); ++nextProjIter) {
if (*vIter == *nextvIter)
finalEnd = std::remove(vIter, finalEnd, *nextvIter);
}
}
v.erase(finalEnd, v.end());
for(auto p : v)
cout << p << " ";
//Should return: 1 7 2 3 8 4 5 6 9 10
return EXIT_SUCCESS;
}