Deleting an element from a vector

This is a discussion on Deleting an element from a vector within the C++ Programming forums, part of the General Programming Boards category; I'v echecked before how to delete a vector entry, but that was to go through the whole vector one element ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Deleting an element from a vector

    I'v echecked before how to delete a vector entry, but that was to go through the whole vector one element at a time. I was wandering if I can erase the last vector entry if it's equal to something in particular.
    I'm trying this:
    Code:
              if(strncmp(SAD.vectDL[SAD.vectDL.size() - 1].Param, "ENDL", 4) == 0)
    			{
    				iterator = vars.vectParmSelectedS.begin();
    
    				while(iterator != vars.vectParmSelectedS.end())     
    				{
    					if(strncmp(SAD.vectDL[SAD.vectDL.size() - 1].Param, "ENDL", 4) == 0)
    						iterator = vars.vectParmSelectedS.erase(iterator);
    					else
    						++iterator;
    
    				}
    			}
    So if the last entry in the vector is "ENDL" I want to delete it. In this case I got hrough the whole vector looking for that entry again.
    There must be a less painful way to delete an entry. Ex: can I use the index of the last entry somehow?
    to avoid doing all of this, is there a swap function that I can use to swap the last entry with another on in the vector?
    Everything is relative...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Take a look at back() and pop_back().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Code:
    vector<T> v;
    T target;
    
    if(v.back() == target)
    {
         v.erase(v.back(), v.end());
         // or simply v.erase(v.back());
    }
    Does that work for you?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I actually thought it would be safer to delete all instances of that entry so I used:
    Code:
    vector <DataLine>::iterator iterator1 = SAD.vectDL.begin();
    
    	while(iterator1 != SAD.vectDL.end())     
    	{
    		if(iterator1->Param == "ENDL")
    			iterator1 = SAD.vectDL.erase(iterator1);
    		else
    			++iterator1;
    
    	}
    but pop-back is very suefull.

    Thanks,

    AS.
    Everything is relative...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21