以 C++ 为单位计算有序集的并集
Calculate the union of an ordered set in C++
我想组合运行长度编码方案的三种变体(运行长度是累积的,因此是变体)。
让我们从其中两个开始:
第一个包含布尔值列表,第二个包含计数器列表。假设第一个看起来如下:(值:该值的位置):
[(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)
可能会更快。
你可以在这里看到它的实际效果。
- 以天C++为单位的两个时间戳之间的差异
- 如何以毫秒为单位获取开始时间和 now() 之间的毫秒差异(以 C++为单位?
- 从原始字节解码协议缓冲区(以 C++为单位)
- 如何在 c++ 中确定一条指令(以字节为单位)在哪里结束,另一条指令从哪里开始?
- 编写以 C++ 为单位返回值的函数
- 以C++为单位进行运行长度编码
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- arr[n] 是否以 C++ 为单位打印数组的长度?
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 以 C++ 为单位具有输出限制的排列
- 以 GDB 为单位指定浮点精度
- 整数数据如何以位为单位存储在内存中?不是右对齐吗?
- 如何在没有硬编码的情况下以C++为单位获取类数组的长度?
- 指针引用的生存期(以 C++为单位)
- 以字符为单位设置控制台大小
- 获取嵌套 stl 容器的大小(以字节为单位)
- 有没有一种 STL 方法可以找到字符串的所有排列,给出一个以 C++ 为单位的大小?
- 二进制数组中最大连续 1 的起始和结束索引,以 C++ 为单位
- 正向声明的枚举,默认值以.h为单位
- 常量"C"占用的空间(以字节为单位)