你总是可以std ::将每个数据成员绑定到一个std :: tuple并使用字典顺序来排序指向大数据结构的指针向量.然后,您可以在复制输出之前对该数据结构执行std :: unique.通过小修改,您还可以通过直接对大项目向量进行排序来删除重复项.
#include
#include
#include
// tuples have builtin lexicographic ordering,
// I'm assuming all your Item's data members also have operator<
bool operator
{
return std::tie(lhs.first_data, /*...*/ lhs.last_data) < std::tie(rhs.first_data, /*...*/ rhs.last_Data);
}
int main()
{
// In the Beginning, there was some data
std::vector vec;
// fill it
// init helper vector with addresses of vec, complexity O(N)
std::vector pvec;
pvec.reserve(vec.size());
std::transform(std::begin(vec), std::end(vec), std::back_inserter(pvec), std::addressof);
// sort to put duplicates in adjecent positions, complexity O(N log N)
std::sort(std::begin(pvec), std::end(pvec), [](Item const* lhs, Item const* rhs){
return *lhs < *rhs; // delegates to operator< for Item
});
// remove duplicates, complexity O(N)
auto it = std::unique(std::begin(pvec), std::end(pvec), [](Item const* lhs, Item const* rhs){
return *lhs == *rhs; // assumes Item has operator==, if not use std::tuple::operator==
});
pvec.erase(it, std::end(pvec));
// copy result, complexity O(N)
std::vector result;
result.reserve(pvec.size());
std::transform(std::begin(pvec), std::end(pvec), std::back_inserter(result), [](Item const* pelem){
return *pelem;
});
// And it was good, and done in O(N log N) complexity
}