Thread: Deleting an element from a vector

  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
    28,413
    Take a look at back() and pop_back().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01:26 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM