【发布时间】:2018-03-13 09:38:44
【问题描述】:
我想确定向量中是否存在重复项。这里最好的选择是什么?
sort(arrLinkToClients.begin(), arrLinkToClients.end(), [&](const type1& lhs,const type1& rhs)
{
lhs.nTypeOfLink < rhs.nTypeOfLink;
});
auto it = unique(arrLinkToClients.begin(), arrLinkToClients.end(), [&](const type1& lhs, const type1& rhs)
{
lhs.nTypeOfLink == rhs.nTypeOfLink;
});
//how to check the iterator if there are duplicates ?
if (it)
{
//
}
【问题讨论】:
-
std::unique在做什么以及返回什么? -
如果问题只是“是否有 any 重复项”,
std::unique是多余的,因为它会删除 all 重复项。这就是为什么您会得到建议std::adjacent_find的答案,它会在找到第一个重复项时返回。 -
Determining if an unordered vector<T> has all unique elements 的可能重复项。请注意,@UdayrajDeshmukh 链接的问题被标记为我链接的问题的副本。