Results 1 to 7 of 7

Thread: How to remove multiple elements of a vector in a loop

  1. #1
    Join Date
    Sep 2010
    Posts
    71

    How to remove multiple elements of a vector in a loop

    I was devekloping a program in c++ for an vector. My code is std::vector<int> isVector; isVector[0] = 4; isVector[1] = 5; isVector[2] = 6; in that I have remove certain value in the case of 1 and 2 under some condition the value is removed. I had used the following code for it.
    Code:
    for(j=0; j<isVector.size(); j++)
     {
       if(isVector[j] == 5 || isVector[j] == 6) {
          isVector.erase( isVector.begin()+j );
       }
    }
    Which give an output as follow 
    isVector.size() = 2
    isVector[0] = 4;
    isVector[1] = 6;
    it is an fall through problem how should I remove it and I want to change the value of the vector recursively

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: How to remove multiple elements of a vector in a loop

    I had search for your problem. In isVector[j] == 4 in that the control is fall. Even I was facing the similar issue in my program when vectorint[j] ==5, isvector.begin()+i= 0+1 is getting erased. When isVector[j] == 6, isVector.begin()+j = 0+2 and isVector[3] is erased here as value for [2] is erased that’s why the value 5 seen in the list. Maybe this is because of the iterator. I don’t know how to change its dynamic position and I want to know how to remove the value of a vector recursively which should change the size of the vector.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: How to remove multiple elements of a vector in a loop

    I think so you need a different loop. For that you just need to repeat the value of a j in the case where the value of j is removed. I had used this when I was using the large vector where I need to remove the value of any element. Maybe you just need to set the value of j as an 0 after the value id being removed and then again repeat from the top of the main part of the program. Hope this will work as it is for the large vector.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: How to remove multiple elements of a vector in a loop

    You can use this in your program. When you are removing the value from the index you must use the erase-remove. You can erase the value from the loop by using the following program.
    Code:
    #include <iostream>
    #include <algorithm>
    #include <iomanip>
    #include <vector>
    #include <iterator>
    using namespace std1;
    bool IsOdd(int value)
    {
        return value%2 != 0;
    }
    int main()
    {
        vector<int> c;
        c.push_back(1);
        c.push_back(2);
        c.push_back(3);
        c.push_back(4);
    copy(c.begin(),c.end(),ostream_iterator<int>(cout," ")); 
    c.erase( remove(c.begin(),c.end(),3) , c.end() );
     copy(c.begin(),c.end(),ostream_iterator<int>(cout," ")); 
     c.erase( remove_if(c.begin(),c.end(),IsOdd) , c.end() );
     copy(c.begin(),c.end(),ostream_iterator<int>(cout," ")); 
    for (vector<int>::iterator is=c.begin(); is!=c.end(); 
     {
            if (*is == 4)
                is = c.erase(is);
            else
                ++is;
        }
        copy(c.begin(),c.end(),ostream_iterator<int>(cout," ")); 
        return 0;
    }

  5. #5
    Join Date
    Mar 2010
    Posts
    154

    Re: How to remove multiple elements of a vector in a loop

    Even I persist with the same issue. For removing the multiple elements from a loop it will need a extra iteration in a loop so that they won’t have more the 3 element you can try this in your for loop.
    Code:
    for(j=0; j<isVector.size(); j++) {
       if(isVector[j] == 4 || isVector[i] == 5) {
          int remove = isVector[j];
          isVector.erase( remove(isVector.begin(), isVector.end(), remove), isVector.end() );
          i = 0;
    Just try this in your for loop hope this will help you out.

  6. #6
    Join Date
    May 2009
    Posts
    529

    Re: How to remove multiple elements of a vector in a loop

    Even I was facing the same sort of issue and had tried the couple of things but nothing had being compiled successfully. I’m searching something which makes the things simple for me and even I was searching for the documentation but I dint come up with anything. I had even tried a unary predict which take an element as a range of an arguments and then return the value as zero, false or truth. This is due to the pointer or the function whose class is overloading.

  7. #7
    Join Date
    Mar 2010
    Posts
    145

    Re: How to remove multiple elements of a vector in a loop

    For this you just need expressions to be translated. Usually unary predict which is an function takes one argument as an element and then arrange it as an case for which the function argument should be match exactly with the container which represent the production with either zero or false or it might be true value for some of the condition the function should return a Bool value don’t matter whether the element is removed or no. moreover This can be a pointer for a function whose class is overloading an operator () .

Similar Threads

  1. Multiple commands for a batch file in a for loop?
    By SANDESH49 in forum Software Development
    Replies: 4
    Last Post: 08-05-2012, 10:40 AM
  2. Multiple commands in batch file loop
    By Toshaan in forum Vista Help
    Replies: 10
    Last Post: 08-05-2012, 10:39 AM
  3. How to Add and Remove HTML elements Dynamically In Javascript?
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 20-02-2010, 06:48 PM
  4. Remove repeated elements in an ArrayList
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 17-02-2010, 05:26 AM
  5. DOM elements : remove with jQuery
    By Servant in forum Software Development
    Replies: 4
    Last Post: 30-01-2010, 10:32 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,935,817.24421 seconds with 17 queries