以 C++ 为单位计算有序集的并集

Calculate the union of an ordered set in C++

本文关键字:C++ 为单位 计算      更新时间:2023-10-16

我想组合运行长度编码方案的三种变体(运行长度是累积的,因此是变体)。
让我们从其中两个开始:
第一个包含布尔值列表,第二个包含计数器列表。假设第一个看起来如下:(值:该值的位置):

[(true:6), (false:10), (true:14), (false:20)]
// From 1 to 6, the value is true
// From 7 to 10, the value is false
// From 11 to 14, the value is true
// From 15 to 20, the value is false

第二个如下所示(再次(值:该值的位置)):

[(1:4), (2:8), (4:16), (0:20)]
// From 1 to 4, the value is 1
// From 5 to 8, the value is 2
// From 9 to 16, the value is 4
// From 17 to 20, the value is 0

如您所见,两种情况下的位置略有不同:

Case 1 : [6, 10, 14, 20]
Case 2 : [4, 8, 16, 20]

我想通过计算它们的联合来组合这些"位置数组":

[4, 6, 8, 10, 14, 16, 20]

一旦我有了这个,我会从那里得出新的方案:

[(true:4), (true:6), (false:8), (false:10), (true:14), (false:16), (false:20)]
[(1:4), (2:6), (2:8), (4:10), (4:14), (4:16), (0:20)]

我想知道:是否有任何C++标准类型/类可以包含"数组"[6、10、14、20] 和 [4, 8, 16, 20],计算它们的联合并对其进行排序?

谢谢
多米尼克

您需要

使用<algorithm>中的std::set_union

我在这里使用std::vector<int>,但它可以是任何模板类型。

#include <iostream>
#include <array>
#include <algorithm>
int main() {
  std::vector<int> a{6, 10, 14, 20};
  std::vector<int> b{4, 8, 16, 20};
  std::vector<int> c;
  std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(c));
  for(auto e: c) {
    std::cout << e << ' ';
  }
  std::cout << 'n';
}

这是 ideone

如果你想只维护两个std::vector而不引入c,你可以简单地将b附加到a,对数组进行排序,然后在a上调用std::unique。在O(n)可能有一种聪明的方法可以做到这一点,但这是天真的方法:

#include <iostream>
#include <algorithm>
#include <vector>
int main() {
  std::vector<int> a{6, 10, 14, 20};
  std::vector<int> b{4, 8, 16, 20};
  a.insert(a.end(), b.begin(), b.end());
  std::sort(a.begin(), a.end());
  auto last = std::unique(a.begin(), a.end());
  a.erase(last, a.end());
  for(auto e: a) {
    std::cout << e << ' ';
  }
  std::cout << 'n';
}

这是 ideone

最后,您可以使用std::inplace_merge而不是std::sort。在最坏的情况下,它像std::sort一样O(nlogn),但在最好的情况下它是O(n) .性能大幅提高:

#include <iostream>
#include <algorithm>
#include <vector>
int main() {
  std::vector<int> a{6, 10, 14, 20};
  std::vector<int> b{4, 8, 16, 20};
  auto a_size = a.size();
  a.insert(a.end(), b.begin(), b.end());
  // merge point is where `a` and `b` meet: at the end of original `a`.    
  std::inplace_merge(a.begin(), a.begin() + a_size, a.end());
  auto last = std::unique(a.begin(), a.end());
  a.erase(last, a.end());
  for(auto e: a) {
    std::cout << e << ' ';
  }
  std::cout << 'n';
}

这是 ideone

我想知道:是否有任何C++标准类型/类可以包含"数组"[6、10、14、20] 和 [4, 8, 16, 20],计算它们的联合并对其进行排序?

我猜你在问这个问题之前没有做太多研究。有一个管理有序集的类模板,称为 set 。如果将两个集合的所有元素添加到一个集合中,则将具有并集。

std::set<int> s1{6, 10, 14, 20};
std::set<int> s2{4, 8, 16, 20};
std::set<int> union = s1;
union.insert(s2.begin(), s2.end());

正如 erip 所暗示的,有一种算法只需要你迭代两个向量一次。作为前提条件,它们都必须在开始时进行排序。您可以使用该事实始终检查哪个更小,并且仅将该向量中的值附加到结果中。它还允许您删除重复项,因为如果要添加一个值,则该值只有在添加到结果向量的最后一个值时才是重复值。

我已经编写了一些代码;我还没有对它进行广泛的测试,所以它可能仍然有点问题,但你来了:

// Assume a and b are the input vectors, and they are sorted.
std::vector<int> result;
// We know how many elements we will get at most, so prevent reallocations
result.reserve(a.size() + b.size());
auto aIt = a.cbegin();
auto bIt = b.cbegin();
// Loop until we have reached the end for both vectors
while(aIt != a.cend() && bIt != b.cend())
{
    // We pick the next value in a if it is smaller than the next value in b.
    // Of course we cannot do this if we are at the end of a.
    // If b has no more items, we also take the value from a.
    if(aIt != a.end() && (bIt == b.end() || *aIt < *bIt))
    {
        // Skip this value if it equals the last added value
        // (of course, for result.back() we need it to be nonempty)
        if(result.size() == 0 || *aIt != result.back())
        {
            result.push_back(*aIt);
        }
        ++aIt;
    }
    // We take the value from b if a has no more items, 
    // or if the next item in a was greater than the next item in b
    else
    {
        // If we get here, then either aIt == a.end(), in which case bIt != b.end() (see loop condition)
        // or bIt != b.end() and *aIt >= *bIt.
        // So in either case we can safely dereference bIt here.
        if(result.size() == 0 || *bIt != result.back())
        {
            result.push_back(*bIt);
        }
        ++bIt;
    }
}

允许在风格和性能上进行一些优化,但我认为它总体上有效。

当然,如果你想把结果恢复到a,你可以修改这个算法直接插入到a中,但保持这样,最后a.swap(result)可能会更快。

你可以在这里看到它的实际效果。