将一个大型已排序的QVector合并为两个大型已排序的QVector的最佳(最快)方法是什么?

我有以下代码:

class Square
{
    .....
    qint32 id; //public
    .....
}

QVector <Square> v_one; //size 10000+
QVector <Square> v_two; //size 10000+

我已经按“v_one”对v_twoid进行了排序。

如何通过按 v_one = v_one + v_two 排序,将这两个 vector 进行 FAST 合并为其中一个(例如id)。

我想我必须作为一个 Action (排序和合并)而不是一个接一个地执行此操作?

谢谢!

最佳答案

如果您想将它们合并为两个 vector 之一,则建议std::inplace_merge:

auto size_one = v_one.size();
v_one += v_two;
std::inplace_merge(v_one.begin(), v_one.begin() + size_one, v_one.end(),
  [](Square const &a, Square const &b) -> bool
  { return a.id < b.id; });

对于并行执行:实验的C++ Extensions for Parallelism, ISO/IEC TS 19570:2015具有 std::experimental::parallel::inplace_merge ,将来可能会成为标准的一部分。您可以在implementation for the parallel merge algorithms中找到CodePlex Parallel STL project,它是Microsoft Parallelism Extension的原型(prototype)。

编辑:

可以使用std::unique删除重复项。
auto new_end = std::unique(v_one.begin(), v_one.end(),
  [](Square const &a, Square const &b) -> bool
  { return a.id == b.id; });
v_one.erase(new_end, v_one.end());

关于c++ - 连接两个大型QVector并对其进行排序的最快方法(C++/Qt),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35285108/

10-11 22:37
查看更多