Remove vector elements based on the index

怎甘沉沦 提交于 2019-12-01 05:51:53

Online Demo:

#include <vector> 
#include <algorithm>
#include <iostream>

/*Check if Index is Even or Odd*/ 
bool is_IndexEven(int i) 
{
   static int k = 1;

   /*Handle Index 0 as special case as per choice*/
   if(k == 1)
   {
       k++;
       return false;
   } 

   if(k++ % 2)
       return true;
   else 
       return false; 
 }

int main() 
{
    using namespace std;
    int elements[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    // create a vector that holds the numbers from 0-9.
    vector<int> v(elements, elements + 10); 

    /*Display elements before removal*/    
    vector<int>::const_iterator iter = v.begin();
    cout<<"Before\n";

    for(iter;iter!= v.end();++iter)
    {
        cout<<*iter;
    }

    /*Remove_if + Erase Algorithm for one step removal*/
    v.erase( remove_if(v.begin(), v.end(), is_IndexEven), v.end() ); 

    /*Display result after elements removed*/
    cout<<"\nAfter\n";
    iter = v.begin();
    for(iter;iter!= v.end();++iter)
    {
       cout<<*iter;
    }

    return 0;
}
JosephH

By going from 0 to size, you'll end up skipping half of the elements because the indices change as you erase the elements. Make your for loop go from size() to 0:

for(unsigned int i = line.size(); i > 0; i--)
{

}

Why don't you use remove_if? Use a static variable inside the function to signal the index for the current element.

Here is how to use erase-remove method to remove odd numbers from a vector. I am not sure whether you can remove elements based on the index, because remove_if() applies the predicate on the values pointed by the iterators rather than the iterator itself.

See the following : http://cplusplus.com/reference/algorithm/remove_if/

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    vector<int> v;
    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
    v.push_back(44);
    v.push_back(55);
    v.push_back(66);
    v.push_back(77);
    ostream_iterator<int> printit(cout, " ");

    cout << "Before removing odd numbers" << endl;
    copy(v.begin(), v.end(), printit);


    v.erase(remove_if(v.begin(), v.end(),
          [] (int e) { return e%2 == 1; }), v.end());

    cout << endl;
    cout << "After removing odd numbers" << endl;
    copy(v.begin(), v.end(), printit);
    cout << endl;
}
Nikos Athanasiou

An answer that generalizes not only upon the type of container to be handled but also upon the type of container that holds the indices to be removed, is given in : Erasing elements in stl::vector by using indexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!