erase in vector

This is a discussion on erase in vector within the C++ Programming forums, part of the General Programming Boards category; How do you erase something in a vector? My book tells me to use pop_back, but from what I've been ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    erase in vector

    How do you erase something in a vector? My book tells me to use pop_back, but from what I've been reading and trying out it removes the last object of my vector. I've tried erase, but I can't make it work!

    Also, once this does work, will my if statement be ok if I add it to the code?

    Any help is appreciated as always.

    Code:
    for(i=0; i<=Process.size()-1; i++)
    	{
    
    			if (lastName == Access[i].lastName)
    
    
    
    //				&&
    //				Access[i].firstName == firstName &&
    //				Access[i].middleIni == middleIni)
    			{
    				
    				Access.erase(i);
    
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can use erase().

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    I've tried that but it doesn't work.

    erase can not be used on it's own, at least not in my code.

    I've tried Access.erase() and Access.erase(i) (which is what I want to do, is erase i) but I get a compiler error.

    I don't get it though. When I tried Access.erase(0) just to see what parameter it would accept, it worked. But I'm obviously looking for a specific subscript, not the first one. Since 'i' evaluates to an integer just as 0 does, why doesn't it take i?

    I don't know where to look for answers anymore. I've looked in my books, on this site, on the web.....I am totally stumped. I know I ask a lot of questions, but I do try very hard before I post and I've got to tell you, this place is one of the best place to learn in my opinion.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Oh, and I've tried what my book says, which is

    Access[i] = Access[i+1];
    Access.pop_back();

    it seemed to work at times, but it mostly doubles my last entry and doesn't always delete what it's supposed to delete.

    Again, any comments and suggestions are welcome.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Okay, as far as I know pop_back() will remove information in first in last out order. So you can't just delete an arbitrary value. Here is a sample of erase.

    Code:
    vector<int> array;
    
    // add some stuff to the vector
    array.push_back(1);
    array.push_back(2);
    array.push_back(3);
    array.push_back(4);
    array.push_back(5);
    array.push_back(6);
    array.push_back(7);
    array.push_back(8);
    array.push_back(9);
    array.push_back(10);
    
    array.erase(array.begin() + 6);
    [edit]
    I deleted the 6th entry.
    [/edit]

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Thanks for your suggestion Master, it's getting me closer.... but it's still doesn't work.... I don't mean to be a pest, but you seem to know how this works...

    Here is the whole code segment. Thing is, I'm looking for the element to erase and then trying to erase it.... maybe that's the problem? It has got to erase i.... so I've tried it this way...

    I get an assertion fault when I try to run it. It compiles fine on VC++.

    [code]

    void Delete(vector<Names>& Access, vector<Entry>& Process, int& i)
    // Pre condition: Vector size is known
    // Post condition: Deletes an entry

    {
    string firstName;
    string lastName;
    string middleIni;

    cout << "\nYou have chosen to delete an entry.\n\n";
    cout << "Please enter the first name: ";
    cin >> firstName;
    cout << "\n\nPlease enter the middle initial: ";
    cin >> middleIni;
    cout << "\n\nPlease enter the last name: ";
    cin >> lastName;



    for(i=0; i<=Process.size()-1; i++)
    {

    cout << lastName << Access[i].lastName;
    if (lastName == Access[i].lastName)
    // && Access[i].firstName == firstName &&
    // Access[i].middleIni == middleIni)
    {

    cout << "BOO";

    }

    }
    Access.erase(Access.begin() + i);


    Menu(Access, Process, i);

    } // End Delete

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why are you passing in i as a parameter (let alone why is it being passed by reference)? Also, shouldn't

    Code:
    Access.erase(Access.begin() + i);
    
    
    Menu(Access, Process, i);
    be inside the for loop? I believe thi s is the problem.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It just occured to me your problem is that you should have a break statement inside the if statement. Everything I see looks fine (within this context). Your problem may be in another function.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    I've put it inside the loop.

    Well, I've been trying for hours! I'm just plain out of ideas.....

    The break statement doesn't seem to do anything. Whether it's there or not it acts the same.

    The

    Access.erase(Access.begin() + i);

    compiles, but it doesn't erase anything.

    The only way I got any deletion was with pop_back, which I've tried again, and I got it to delete in the middle of the vector, but with very weird results.

    If anyone can help so I can stop pulling my hair out, I'd appreciate it.
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  10. #10
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Use a combination of erase and remove......
    Code:
    vector<int> vec;
    .
    .
    .
    vec.erase(remove(vec.begin(),vec.end(),ValToBeErased),vec.end());
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    How do you call this function from somewhere else? What I am trying to get at is what would you put into the third parameter? I would think it would work like so:
    Code:
    void Delete(vector<Names>& Access, vector<Entry>& Process)
    // Pre condition: Vector size is known
    // Post condition: Deletes an entry
    
    {
       string firstName;
       string lastName;
       string middleIni;
    
       cout << "\nYou have chosen to delete an entry.\n\n";
       cout << "Please enter the first name: ";
       cin >> firstName;
       cout << "\n\nPlease enter the middle initial: ";
       cin >> middleIni;
       cout << "\n\nPlease enter the last name: ";
       cin >> lastName;
    
       for(int i=0; i<=Process.size()-1; i++)
       {
          if (lastName == Access[i].lastName && Access[i].firstName == firstName && Access[i].middleIni == middleIni)
         {
             Access.erase(Access.begin() + i);
             Process.erase(Process.begin()+i);  // not sure if you wanted to delete process as well
             break;
         }
       }
    
       Menu(Access, Process, i);
    
    } // End Delete
    Let me know if this works
    Last edited by PJYelton; 11-21-2002 at 09:52 AM.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Master that third parameter shouldn't of been there, that was my bad.

    Thanks , Stoned I'll keep that snippet in mind, except that it told me that

    Code:
    vec.erase(remove(vec.begin(),vec.end(),ValToBeErased),vec.end());
    vec.erase didn't take 3 parameters.

    Thanks PJ! Of course it worked I truly appreciate it.


    And yes, I'm calling the function from a menu function.

    I'm almost done with the whole thing, it works ok, but it's not as robust as I would like it to be..... but I think I've passed the major hurdles. It's not very elegant, but it will do.

    Thanks everyone!!

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I see only 2 params. maybe you typed it wrongly.
    param1= the return value of remove.
    param2= the iterator returned by end()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    81
    Sorry Stoned, my bad.... it was remove that didn't accept 3 parameters... I appreciate your comments... Like the name too

  15. #15
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    did u #include<algorithm>?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 1 of 2 12 LastLast
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, 09:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 06:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

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