I have std::vector<string> listItems
i want to remove duplicate values not changing the order of the strings
eg abc has "B","C","E","D","C"
I need "B","C","E","D" in the list
SOrt & erase are changing the order
std::sort(listItems.begin(), listItems.end());
listItems.erase(unique( listItems.begin(), listItems.end() ), listItems.end() );
Go through the vector, keeping track of what you've already seen. If you've already seen that value, erase it.
I haven't built or tested this code, but it should give you a rough idea. There are many many ways to do this; this code is just a simple, easy to understand demonstration. We just go through the vector, keeping track of values already seen, and if we've already seen it, erase from the vector.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
std::set<string> alreadySeen;
for (int i = 0 ; i < listItems.size() ; ++i)
{
if (alreadySeen.find(listItems[i]) == alreadySeen.end())
{
// haven't seen this one before,
// now we have seen it so add
// it to the set of ones we've already seen
alreadySeen.insert(listItems[i]);
}
else
{
// we have already seen this one, so erase this duplicate
listItems.erase(listItems.begin() + i);
i--; // we need to decrement i so that we will check the one we just moved into the space where the one that was erased used to be
}
}