从映射中删除键不包含在另一个向量中的元素
Remove elements from map with keys not containing in another vector
给定一个 stl 容器,比如 std::map:
std::map<int, CustomClass> some_container;
std::vector<int> real_objects;
如何正确有效地从地图中删除some_container键不在矢量real_objects元素?地图是此类任务的最佳容器吗?
我的第一直觉是批量擦除非真实对象块:
// Assuming real_objets is sorted (otherwise sort it)
auto first = some_container.begin();
for(int i : real_objects) {
// end of the chunk to erase is the place where i could be
// inserted without breaking the order
auto last = some_container.lower_bound(i);
some_container.erase(first, last);
// if i is a key in the container, skip that element
if(last != some_container.end() && last->first == i) {
++last;
}
first = last;
}
// and in the end, kill stuff that comes after real_objects.back()
some_container.erase(first, some_container.end());
这具有运行时复杂度O(n * log(m)),其中n是real_objects.size()
,m是some_container.size()
,这意味着如果some_container
远大于real_objects
,它的性能最佳。否则,由于可以在线性时间内循环访问std::map
,因此您可以按顺序步调遍历两者并删除差异:
// again, sort real_objects.
if(!some_container.empty()) { // else there's nothing to do
auto ir = real_objects.begin();
auto ire = std::lower_bound(real_objects.begin(),
real_objects.end (),
some_container.rbegin()->first);
auto is = some_container.begin();
for(;;) {
// find the next place where the real_objects and the keys of some_container don't match
auto p = std::mismatch(ir, ire, is, [](int i, std::pair<int, int> const &p) { return i == p.first; });
if(p.first == real_objects .end() ||
p.second == some_container.end())
{
// upon reaching the end, remove all that comes after the
// end of real_objects (if anything)
some_container.erase(p.second, some_container.end());
break;
} else if(*p.first < p.second->first) {
// if there's something in real_objects that's not in
// some_container, skip it
++p.first;
} else {
// otherwise there's something in some_container that's
// not in real_objects, so delete it.
p.second = some_container.erase(p.second);
}
ir = p.first;
is = p.second;
}
}
这具有运行时复杂度 O(max(n, m)),因此如果 some_container
和 real_objects
几乎匹配,它应该表现良好。
相关文章:
- 将一个向量插入另一个向量的某个位置
- 将指向给定子类的指针从一个向量复制到另一个向量
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 使用 std::transform 将向量向量 (a) 添加到另一个向量向量 (b)
- 如何按另一个向量的方向调整一个向量?
- 将字符向量复制到另一个向量
- 如何将所有指针从一个向量移动到另一个向量?
- 如何从另一个向量中的另一个第一元素减去向量中的第一元素
- 用一个向量替换另一个向量的方法
- 如何根据另一个向量中给出的索引拆分向量?
- 按另一个向量对一个向量进行排序
- 向量迭代器不兼容的错误,用于保存另一个向量的迭代器的向量
- 如何从一个向量中删除最小元素并添加到另一个向量,而第一个变为空?
- C++将 X 向量数据插入到另一个向量
- 将向量{x,y,z}插入另一个向量
- 给出一个 std::vector 对另一个向量的内存的所有权?
- 从另一个向量中搜索和找到元素的有效方法
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃