How to remove duplicate values from a list in c++ [duplicate]
Possible Duplicate:
How can I remove duplicate values from a list in c++?
Hi, I have to remove duplicate values from a list in c++. can any one tell me how to do that as I am new to c++.
Any sample code will be highly appreciated.
Regards Shekhar
std::sort, then std::unique
I think you want STL unique (and some example code as requested).
I haven't made much usage of lists but I envision them to be something like arrays. In that case, one way to do this is to sort them alphabetically and then iterate through the list compare the current element to the next element. If they're the same delete. If not, proceed!
Check this:
http://www.java2s.com/Tutorial/Cpp/0340__list/DemonstratingtheSTLlistuniquefunctions.htm
As said by Raph, use stl unique. as simple as that.
The answer to your question is to used STL function name "Unique", but this function requires the collection to be sorted, so if your collection is not sorted then use STL Sort.
精彩评论