Vector deletion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vsachar
    New Member
    • Mar 2009
    • 15

    Vector deletion

    Hi
    I had a doubt about deleting from vectors. I have three vectors of types: void*, bool and int. I insert into these three vectors together and when the vector of bool type has the value true, I want to delete the elements in that position from all these three vectors. Can anyone please help me do that.


    Code:
    for(unsigned int i =0; i<Ptr.size() ; i++)
    {
    	if(Bool[i]==1)
    	{
    		iP=Ptr.begin();
    		iP=Ptr.erase(iP+i);
    
    		iO=Int.begin();
    		iO=Int.erase(iO+i);
    			
    		iM=Bool.begin();
    		iM=Bool.erase(iM+i);
    			
    	}
    }
    iP, iM and iO are iterators. I tried the above method but only one element got deleted. I know why but I don't know how to fix that.
    Please help.
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    What happens if you delete element at index 0? The rest of the vector shifts and what was at index 1 , now is at index 0. Then you advance the counter, and skip the now-first element. Advance it only when element is not deleted.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      First, a couple of things:

      1) a vector<bool> is not a vector. Some wise guy thought he could implement C bitfields in C++ and failed at it. All vectors are required to be arrays. Therefore, you can always assign the address of a vector element to a pointer:

      Code:
      vector<int> v;
      v.push_back(10);
      int* ptr = &v[0];
      However, this code will not complile for vector<bool>. Refer to Scott Meyers book Effective STL.

      You are supposed to use a bitset for bool values.

      2) You check a bool for true by testing it to be true and not equal to 1.

      bool b = true;

      Code:
      if (b==true) etc...           //OK
      if(b==1) etc...               //not good. May generate warning about coinversion
                                         //of 1 ro bool.
      3) Your vectors are changing size.

      For example, assume the bools are false true true.
      Your loop counter , i, starts at 0. OK.
      Nothing is erased. i increments to 1.
      Now you erase an element. vector::size is now 2.
      i increments to 2.
      You exit the loop.

      Notice that when vector::size() is 2, the elements of your vectors are 0 and 1.

      I suggest you start at vector::begin() and run to vector::end(). You will need to do this until there are no erases. Then you are done.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by weaknessforcats
        I suggest you start at vector::begin() and run to vector::end(). You will need to do this until there are no erases. Then you are done.
        The erase function returns the itterator to the next item in the storage container so I have tended to do this for erase loops

        Code:
        vector<int> vecint;
        vector<int>iterator it;
        
        for( it=vecint.begin(); it!=vecint.begin(); )
        {
          if (<EraseConditionIsTrue>)
          {
            it = vecint.erase(it);
          }
          else
          {
            it++;
          }
        }
        which voids the need to perform multiple loops checking to see if anything has been erased.


        Additionally rather than 3 vectors of basic types it sounds to be like you would be better off with a single vector of a structure or maybe class or a pointer or better yet a handle to one of those.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Better erase from the end to the start for reasons that become obvious once you've seen them, e.g. if I have to erase elements 1 and 2 from a vector with elements v0, v1, v2, v3, after erasing element v1 the vector contains v0, v2, v3 and v3 has become the new second element and will also be erased instead of the value v2 (if you erase from front to back).

          kind regards,

          Jos

          Comment

          • vsachar
            New Member
            • Mar 2009
            • 15

            #6
            What happens if you delete element at index 0? The rest of the vector shifts and what was at index 1 , now is at index 0. Then you advance the counter, and skip the now-first element. Advance it only when element is not deleted.
            Thank you all. It works.

            Also, I think I should use a structure instead of 3 vectors. Thanks for that

            Comment

            Working...