使用索引删除stl :: vector中的元素 [英] Erasing elements in stl::vector by using indexes

查看:616
本文介绍了使用索引删除stl :: vector中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 stl :: vector< int> ,我需要删除所有元素在给定的索引(向量通常具有高维度)。我想知道,这是最有效的方式来做这样的操作,记住原始向量的顺序应该保留。



虽然,我发现相关帖子,其中一些需要删除一个单个元素多个元素,其中 remove-erase成语似乎是一个很好的解决方案。然而,在我的情况下,我需要删除多个元素,因为我使用索引而不是直接值, remove-erase idiom 不能应用,对不对?
我的代码如下,我想知道是否可以做到比效率更好。

  bool find_element(const vector< int>& vMyVect,int nElem){
return(std :: find(vMyVect.begin(),vMyVect.end(),nElem)!= vMyVect.end )?真假;
}

void remove_elements(){

srand(time(NULL));

int nSize = 20;
std :: vector< int> vMyValues;
for(int i = 0; i vMyValues.push_back(i);
}

int nRandIdx;
std :: vector< int> vMyIndexes;
for(int i = 0; i <6; ++ i){
nRandIdx = rand()%nSize;
vMyIndexes.push_back(nRandIdx);
}

std :: vector< int> vMyResult;
for(int i = 0; i<(int)vMyValues.size(); i ++){
if(!find_element(vMyIndexes,i)){
vMyResult.push_back(vMyValues [一世]);
}
}
}


解决方案>

我认为它可以更高效,如果你只是排序你的索引,然后从您的矢量中删除这些元素从最高到最低。删除列表上的最高索引不会使要删除的较低索引无效,因为只有高于删除的索引的元素会更改其索引。



如果它真的更高效的将取决于排序的速度。关于这个solultion的另一个pro是,你不需要你的价值向量的副本,你可以直接工作在原始的向量。代码应该看起来像这样:

  ...填满向量... 

sort (vMyIndexes.begin(),vMyIndexes.end());

for(int i = vMyIndexes.size() - 1; i> = 0; i - ){
vMyValues.erase(vMyValues.begin()+ vMyIndexes [i] )
}


I've a stl::vector<int> and I need to remove all elements at given indexes (the vector usually has high dimensionality). I would like to know, which is the most efficient way to do such an operation having in mind that the order of the original vector should be preserved.

Although, I found related posts on this issue, some of them needed to remove one single element or multiple elements where the remove-erase idiom seemed to be a good solution. In my case, however, I need to delete multiple elements and since I'm using indexes instead of direct values, the remove-erase idiom can't be applied, right? My code is given below and I would like to know if it's possible to do better than that in terms of efficiency?

bool find_element(const vector<int> & vMyVect, int nElem){
    return (std::find(vMyVect.begin(), vMyVect.end(), nElem)!=vMyVect.end()) ? true : false;
}

void remove_elements(){

    srand ( time(NULL) );

    int nSize = 20;
    std::vector<int> vMyValues;
    for(int i = 0; i < nSize; ++i){
            vMyValues.push_back(i);
    }

    int nRandIdx;
    std::vector<int> vMyIndexes;
    for(int i = 0; i < 6; ++i){
        nRandIdx = rand() % nSize;
        vMyIndexes.push_back(nRandIdx);
    }

    std::vector<int> vMyResult;
    for(int i=0; i < (int)vMyValues.size(); i++){
        if(!find_element(vMyIndexes,i)){
            vMyResult.push_back(vMyValues[i]);
        }
    }
}

解决方案

I think it could be more efficient, if you just just sort your indices and then delete those elements from your vector from the highest to the lowest. Deleting the highest index on a list will not invalidate the lower indices you want to delete, because only the elements higher than the deleted ones change their index.

If it is really more efficient will depend on how fast the sorting is. One more pro about this solultion is, that you don't need a copy of your value vector, you can work directly on the original vector. code should look something like this:

... fill up the vectors ...

sort (vMyIndexes.begin(), vMyIndexes.end());

for(int i=vMyIndexes.size() - 1; i >= 0; i--){
    vMyValues.erase(vMyValues.begin() + vMyIndexes[i])
}

这篇关于使用索引删除stl :: vector中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆