简体   繁体   English

是否有 std::vector 的后代可以合并和排序?

[英]Are there descendants of std::vector who can merge and sort?

I'm working with std::vectors for a graphical program.我正在使用 std::vectors 进行图形程序。 Those vectors contain positions on the screen, and they are sorted.这些向量包含屏幕上的位置,并且它们被排序。 Now I'd like to merge them together and keep the actual sorting, while removing possible duplicates, something like this:现在我想将它们合并在一起并保持实际排序,同时删除可能的重复项,如下所示:

vector1 : [2, 6, 10]
vector2 : [1, 5, 6, 10]
result  : [1, 2, 5, 6, 10]

For a good understanding: I have already programmed myself the function to do the actual merging, based on basic std::vector functions, like at() , insert() , size() , but my function seems to be a performance gap (O(n 2 ), I believe).为了更好地理解:我已经根据基本的 std::vector 函数(例如at()insert()size()为自己编写了进行实际合并的函数,但我的函数似乎存在性能差距( O(n 2 ),我相信)。

I'm looking for other std classes (std::vector descendants if possible, for the ease of programming), who contain merge() and sort(kind="unique") as basic methods.我正在寻找其他 std 类(如果可能,std::vector 后代,为了便于编程),它们包含merge()sort(kind="unique")作为基本方法。

Does anybody know if such classes exist in STL?有人知道STL中是否存在这样的类吗?

STL has this concept of separation of containers and algorithms , so while std::vector indeed does not have members to sort or merge it, STL provides all the required algorithms via non-member function templates that handle iterators. STL 具有容器和算法分离的概念,因此虽然std::vector确实没有成员对其进行排序或合并,但 STL 通过处理迭代器的非成员函数模板提供所有必需的算法。

Eg to sort a vector you would call例如,对您会调用的向量进行排序

std::sort(vector1.begin(),vector1.end());

Check algorithm header for further reference, namely std::sort and std::merge .检查算法头以供进一步参考,即std::sortstd::merge

To merge and remove duplicates you can use std::set_union , which is probably your best bet.要合并和删除重复项,您可以使用std::set_union ,这可能是您最好的选择。 Here is working code example .这是工作代码示例

Here is a tutorial on iterators, though for this specific task you would only need self-explanatory vector::begin() and vector::end() .是关于迭代器的教程,但对于此特定任务,您只需要不言自明的vector::begin()vector::end()

For removing duplicates from a single container you would usually use std::unique() or std::unique_copy() , as @unwind mentioned.要从单个容器中删除重复项,您通常会使用std::unique()std::unique_copy() ,如@unwind 所述。

There is one caveat with std::unique() and other "removing" algorithms like std::remove() , which stems from that "separation of containers and algorithms" that I mentioned: an algorithm has no means to actually remove elements from the container - it's been given an iterator or a range, but it has no idea about the actual type and implementation of the container. std::unique()和其他“删除”算法(如std::remove()有一个警告,这源于我提到的“容器和算法的分离”:算法无法实际从中删除元素容器 - 它被赋予了一个迭代器或一个范围,但它不知道容器的实际类型和实现。

So instead common approach is to just move the elements intended for removal to the end of the range, and then return the iterator to the first of these elements.因此,通常的方法是将要删除的元素移动到范围的末尾,然后将迭代器返回到这些元素中的第一个。 Then you can call another function to do actual removal (notice how this time it will be a container method ).然后你可以调用另一个函数来进行实际删除(注意这次它将是一个容器方法)。

This is how it's done with std::unique() :这是如何使用std::unique()

vec.erase(std::unique(vec.begin(), vec.end()), vec.end());

std::unique_copy will not require this trick, but it will pretty much copy the whole vector, so it makes sense only if you intended to copy it anyway. std::unique_copy不需要这个技巧,但它几乎会复制整个向量,所以只有当你打算复制它时才有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM