std :: unique与谓词比较std :: string不删除重复 [英] std::unique with predicate comparing std::string not removing duplicate

查看:211
本文介绍了std :: unique与谓词比较std :: string不删除重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我缺少某些东西或错误的机制(很可能)这个向量中不应该有1重复吗?

Unless I am missing something or missunderstand the mechanism (very likely) Shouldn't the "1" duplicate not exist in this vector ?

chunks.erase( std::unique ( chunks.begin(), chunks.end(), 
                           []( std::string &s1, std::string &s2 ){ 
                              return ( s1.compare(s2) == 0 ? true : false );}), 
                           chunks.end() );

执行上述操作之前:

1       l:1
1+      l:2
1+1     l:3
1+1=    l:4
+       l:1
+1      l:2
+1=     l:3
1       l:1
1=      l:2
=       l:1

执行上述代码后:

1       l:1
1+      l:2
1+1     l:3
1+1=    l:4
+       l:1
+1      l:2
+1=     l:3
1       l:1
1=      l:2
=       l:1

我没有使用谓词(假设std :: strings是相同的将被删除)。由于某些原因,ones被标识为相同?我已经看了他们的长度(假设一个空格被卡住作为前缀或后缀),但他们有相同的长度。

I have tried without a predicate (assuming std::strings that are identical would be removed). For some reason the "ones" are identified as identical? I have looked at their length (assuming a whitespace was stuck as a prefix or postfix) but they have the same length.

我错过了什么?

推荐答案

您可能误解了一些东西。

You are (probably) misunderstanding something.

std :: unique 只删除连续的重复项,所以如果你希望删除所有重复的前提条件,应用 std :: unique 相同的谓词。

std::unique only removes contiguous duplicates, so if you wish to remove all duplicates a precondition to applying std::unique is to sort your range using the same predicate.

这篇关于std :: unique与谓词比较std :: string不删除重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆