Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6922299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:23:18+00:00 2026-05-27T10:23:18+00:00

I wanted to remove the elements of the vector based on the index, say

  • 0

I wanted to remove the elements of the vector based on the index, say all the even indexed elements.
I have read about the erase remove idiom but can’t see how to apply it.
This is what I tried:

    vector<int> line;
    line.reserve(10);
    for(int i=0;i<10;++i)
    {
      line.push_back(i+1);
    }
    for(unsigned int i=0;i<line.size();++i)
    {
      //remove the even indexed elements
      if(i%2 == 0)
      {
        remove(line.begin(),line.end(),line[i]);
      }
    }
line.erase( line.begin(),line.end() );

This erases the entire vector. I was hoping to only remove the elements that had been marked by the remove algorithm.

Then I tried this

for(unsigned int i=0;i<line.size();++i)
    {
      //remove the even indexed elements
      if(i%2 == 0)
      {
        line.erase( remove(line.begin(),line.end(),line[i]),line.end() );
      }
    }

This again doesn’t work as there is a problem while removing, the indices seem to shift whilst iterating over the vector.
What should be the correct approach to accomplish this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:23:18+00:00Added an answer on May 27, 2026 at 10:23 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear All, I used a vector of vector, say vector<vector<int> > no_1_2 to store
I'm using a enhanced for loop over an ArrayList and wanted to remove some
I want to be able to remove multiple elements from a set while I
I have an array of object and I'm looking to remove an element. However,
1) By default configuration elements have their connectionStringName attribute set to LocalSqlServer , and
I wanted to ask, I have an array and I want to eliminate some
Problem: Multiple asynchronously invoked Effect.Appear() calls do not all resolve properly. Background: I have
How would i using Linq remove all <CCInfo> section where their element <CC> does
Please read this statement carefully: let's assume before ANY elements are added to the
I wanted to know if there's a way to alter child elements while isolating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.