从给定索引上的向量中移除元素,顺序无关紧要

Remove elements from vector on given indexes, order does not matter

本文关键字:元素 无关紧要 顺序 向量 索引      更新时间:2023-10-16

我所拥有的是元素的向量,我不在乎它们的顺序。Than I具有要从向量中移除的元素的N索引(每个索引寻址向量中的唯一位置)。我希望尽快移除。

我能想到的最好的办法是将索引存储在集合中(排序索引):

std::set<unsigned int> idxs;
for (int i=0; i<N; ++i)
    idxs.insert(some_index);

然后以相反的顺序迭代集合,并用向量的最后一个元素替换要删除的索引。

std::set<unsigned int>::reverse_iterator rit;
for (rit = idxs.rbegin(); rit != idxs.rend(); ++rit) {
    vec[*rit].swap(vec[vec.size() - 1]);
    vec.resize(vec.size() - 1);
}

然而,我在想是否有更有效的方法可以做到这一点,因为集合的使用对我来说有点过头了,我希望完全避免排序

第1版:让我们假设我使用向量,然后对其进行排序。

std::vector<unsigned int> idxs;
for (int i=0; i<N; ++i)
    idxs.push_back(some_index);
std::sort(idxs.begin(), idxs.end());

我可以再推一点吗?

第2版:我应该提到,向量将有多达10个元素。然而,我的程序中的删除经常发生(数十万次)。

set是一个不错的选择。我想使用另一个分配器(例如arena)会产生最大的影响。为什么不使用元素的集合而不是向量来开始呢?

我看到以下相关变化:

  • 创建一个新的矢量并复制保留的元素,然后交换回,而不是移除
    这可以保持索引的稳定(不像删除那样需要对索引进行排序或更新)。

  • 使用与数据长度相同的布尔向量,而不是索引向量。给定"最大10"的长度,一个比特掩码似乎就足够了

所以,大致来说:

struct Index 
{
   DWORD removeMask = 0;  // or use bit vector for larger N
   void TagForRemove(int idx) { removeMask |= (1<<idx); }
   boll DoRemove(int idx) const { return (removeMask & (1<<idx)) != 0; }
}
// create new vector, or remove, as you like
void ApplyRemoveIndex(vector<T> & v, Index remove)
{
   vector<T> copy;
   copy.reserve(v.size());
   for (i=0..v.size())
     if (!remove.DoRemove(i))
       copy.push_back(v[i]);
   copy.swap(v);
}

您可以使用swap/pop_back删除给定索引处的项,并使用哈希表跟踪移动的索引。它是线性空间&清除次数中的时间。

std::vector<T> vec = ...;
std::vector<unsigned int> idxs;
std::unordered_map<unsigned int, unsigned int> map;
for(auto index : idxs) {
  unsigned int trueIndex = index;
  while (trueIndex >= vec.size()) {
    trueIndex = map[trueIndex];
  }
  // element at index 'vec.size()-1' is being moved to index 'index'   
  map[vec.size()-1] = index; 
  swap(vec[trueIndex], vec[vec.size()-1]);
  vec.pop_back();   
}