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.

vector<Person>::iterator where = find(Book.begin(), Book.end(), Name);
(*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

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:

void Delete(std::string Name, std::vector<Person>& Book)
{
    for (int i=0; i!=Book.size(); i++)
    {
      if (Book[i].Name == Name)
      {
          Book.erase(Book.begin()+i,Book.begin()+1+i);
          break;
      }    
    }
}

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

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

void Delete(std::string Name, std::vector<Person>& Book)
{
   vector<Person>::iterator vItr = Book.begin();
   while ( vItr != Book.end() )
    {
        if ( vItr->Name == Name )
        {
             vItr = Book.erase( vItr ); // Will return next valid iterator
              break;
        }
        else
             vItr++;

}

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

void Delete(std::string Name, std::vector<Person>& Book)
{
   vector<Person>::iterator vItr = Book.begin();
   while ( vItr != Book.end() )
    {
        if ( vItr->Name == Name )
        {
             vItr = Book.erase( vItr ); // Will return next valid iterator
              break;
        }
        else
             vItr++;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.