小编sks*_*kst的帖子

vector :: erase和reverse_iterator

我在std :: vector中有一个元素集合,它们从第一个元素开始按降序排序.我必须使用向量,因为我需要将元素放在连续的内存块中.我有一个集合,其中包含许多具有所述特征的向量实例(总是按降序排序).

现在,有时,当我发现我在更大的集合中有太多元素(持有这些向量的元素)时,我丢弃这些向量中的最小元素,类似于这个伪代码:

grand_collection: collection that holds these vectors
T: type argument of my vector
C: the type that is a member of T, that participates in the < comparison (this is what sorts data before they hit any of the vectors).

std::map<C, std::pair<T::const_reverse_iterator, std::vector<T>&>> what_to_delete;
iterate(it = grand_collection.begin() -> grand_collection.end())
{
     iterate(vect_rit = it->rbegin() -> it->rend())
     {
         // ...
          what_to_delete <- (vect_rit->C, pair(vect_rit, *it))
          if (what_to_delete.size() > threshold)
               what_to_delete.erase(what_to_delete.begin());
         // ...  
     }
}
Run Code Online (Sandbox Code Playgroud)

现在,在运行此代码之后,what_to_delete我有一组迭代器指向我想从这些向量中移除的原始向量(总体最小值).请记住,原始向量在它们命中此代码之前进行排序,这意味着对于任何what_to_delete[0 - …

c++ iterator stl vector

6
推荐指数
1
解决办法
7899
查看次数

标签 统计

c++ ×1

iterator ×1

stl ×1

vector ×1