开发者

How to remove duplicate values from a list in c++ [duplicate]

This question a开发者_如何学运维lready has answers here: Closed 12 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜