942,515 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17737
  • C++ RSS
Mar 24th, 2008
0

Searching for and deleting an element in vector

Expand Post »
Hi all - hope you can help me with this.

I'm trying to search through a vector of struct's; I'm able to loop through the vector using a simple for() loop, but i'm unable to use the generic find(). Secondly i want to delete a element
if there's a match.

Below is what I have so far (sorry about the big chunk of code, but i'm a newbie so my problem might be how the vector is passed or something - hence the whole code)

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>  // For find

using namespace std;
struct Person
{
    std::string Name;
    std::string Address;
    int Phone;
};

void ChangeAdr(string Name, string Adr, vector<Person>& Book);
void Delete(string Name, vector<Person>& Book);

int main(int argc, char *argv[])
{
    vector<Person> Book;
    string Name, Adr;
    
    // Add contact
    Person x;
    x.Name = "James";
    x.Address = "Bond";
    x.Phone = 007;
    Book.push_back(x);

    // Edit address
    cout << "Enter contacts name: ";
    getline(cin, Name);
    cout << "Enter new address: " ;
    getline(cin, Adr);
    ChangeAdr(Name, Adr, Book);
  
    //Delete Contact
    cout << "Enter contacts name: ";
    getline(cin, Name);
    Delete(Name, Book);  

    return 0;
}

void ChangeAdr(string Name, string Adr, vector<Person>& Book)
{
    for (int i=0; i!=Book.size(); i++)
    {
      if (Book[i].Name == Name)
      {
          Book[i].Address = Adr;
          break;
      }    
    }       
}

void Delete(string Name, vector<Person>& Book)
{
    for(vector<Person>::iterator j = Book.begin();  j != Book.end(); ++j)
   {
      if ((*j).Name == Name)
      {
          delete (*j);  // Dosn't work !
          break;
      }
   }
}

The changeAdr() works fine, but i don't understand why i can't use find()
eg.
C++ Syntax (Toggle Plain Text)
  1. vector<Person>::iterator where = find(Book.begin(), Book.end(), Name);
  2. (*where).Address = Adr;

Regarding delete (*j) the compiler gives the following error:
"main.cpp type `struct Person' argument given to `delete', expected pointer"

Thx in advance

Kind Regards Dan
Last edited by DanLB; Mar 24th, 2008 at 5:45 pm.
Reputation Points: 35
Solved Threads: 0
Newbie Poster
DanLB is offline Offline
4 posts
since Mar 2008
Mar 24th, 2008
0

Re: Searching for and deleting an element in vector

Hi Dan. I tried find() on a similar struct, and I couldn't get it to work either...
I can however help you with the delete problem:

C++ Syntax (Toggle Plain Text)
  1. void Delete(std::string Name, std::vector<Person>& Book)
  2. {
  3. for (int i=0; i!=Book.size(); i++)
  4. {
  5. if (Book[i].Name == Name)
  6. {
  7. Book.erase(Book.begin()+i,Book.begin()+1+i);
  8. break;
  9. }
  10. }
  11. }

There's probably a better way to do it, but i'm a newbie myself
Good luck - hope someone can solve your find problem

Kind regards Kasper
Reputation Points: 10
Solved Threads: 0
Newbie Poster
KasperFP is offline Offline
2 posts
since Mar 2008
Mar 25th, 2008
0

Re: Searching for and deleting an element in vector

Hey guys... find() would work if its a per-defined data type. Since you are having a structure ( user - defined data type you need implement your own find function
C++ Syntax (Toggle Plain Text)
  1.  
  2. void Delete(std::string Name, std::vector<Person>& Book)
  3. {
  4. vector<Person>::iterator vItr = Book.begin();
  5. while ( vItr != Book.end() )
  6. {
  7. if ( vItr->Name == Name )
  8. {
  9. vItr = Book.erase( vItr ); // Will return next valid iterator
  10. break;
  11. }
  12. else
  13. vItr++;
  14.  
  15. }
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Rajith Cherian is offline Offline
22 posts
since Dec 2007
Apr 12th, 2010
0

find

Actually find is a template function so it will work for any type predefined or not. The only requirement is that you implement your equality operator and that's it.

Hey guys... find() would work if its a per-defined data type. Since you are having a structure ( user - defined data type you need implement your own find function
C++ Syntax (Toggle Plain Text)
  1.  
  2. void Delete(std::string Name, std::vector<Person>& Book)
  3. {
  4. vector<Person>::iterator vItr = Book.begin();
  5. while ( vItr != Book.end() )
  6. {
  7. if ( vItr->Name == Name )
  8. {
  9. vItr = Book.erase( vItr ); // Will return next valid iterator
  10. break;
  11. }
  12. else
  13. vItr++;
  14.  
  15. }
Last edited by kay25; Apr 12th, 2010 at 6:46 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
kay25 is offline Offline
3 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Activate void and get string generated there
Next Thread in C++ Forum Timeline: Need serious guidance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC