lundi 20 décembre 2021

Simple program to efficiently remove the elements from vector in C++, Is there any better solution than this?

Is there any more efficient solution than this to remove some elements of the vector?

{
    vector<int> v{1,2,3,4,5,6,7,8,9,10};
 
    for (int i = 0; i < v.size(); i++)
    {
        if(v[i] % 2 == 0)
        {
            auto it2 = std::remove(v.begin(), v.end(), v[i]);
            v.erase(it2);
        }
    }
     
    for (auto it = v.begin(); it != v.end(); it++)
    {
        cout << *it;
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire