2

I have a class that contains 3 elements for example {first_name, Last_name, Phone}

I have a vector that holds this set of information. In what manner could I go about looking for a single element of the set, for example find(last_name), and delete all elements that contain that specific last name?

I've tried many examples and have searched far and wide throughout the world wide google. Please help. Attached is bits of code:

int number = 4;
vector <Friend> BlackBook(number);

Friend a("John", "Nash", "4155555555");
Friend d("Homer", "Simpson", "2064375555");

BlackBook[0] = a;
BlackBook[1] = d;

Now that's just same basic code for the set up. Here's a couple of things i've tried. But the more I look at what the code says, the more it seems as if it's not allowing for a string argument... but then i don't know how to give a class arguement with respect to a specific string... well I don't know what i'm doing wrong. I have a feeling I could do this with pointers, but the whole pointer thing isn't clicking yet. But heres some things i've tried.

vector <Friend> :: iterator frienddlt;
frienddlt = find (BlackBook.begin(), BlackBook.end(), nofriend);
if (frienddlt != BlackBook.end())
{
    BlackBook.erase( std::remove( BlackBook.begin(), BlackBook.end(), nofriend), BlackBook.end() );
}
else
{
    cout << nofriend <<" was not found\n" << "Please Reenter Last Name:\t\t";
}

When I compile the project the header file stl_algo.h opens and points to line 1133. Any Help would be much appreciated!! thank you!

UpAndAdam
  • 4,155
  • 2
  • 25
  • 41
Michael La
  • 51
  • 5
  • You may want to add the `c++` tag to your question to attract the proper audience (and remove the redundant `class`). – WhiteViking Oct 21 '15 at 07:45

2 Answers2

2

Try remove_if

My example:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

struct Friend {
    string first_name;
    string last_name;
    string phone;
};

bool RemoveByName (vector<Friend>& black_book, const string& name) {
    vector<Friend>::iterator removed_it = remove_if( 
        black_book.begin(), black_book.end(), 
        [&name](const Friend& f){return f.first_name == name;});

    if (removed_it == black_book.end())
        return false;

    black_book.erase(removed_it, black_book.end());
    return true;
}

int main() {
    vector <Friend> black_book {
        Friend {"John", "Nash", "4155555555"},
        Friend {"Homer", "Simpson", "2064375555"}
    };
    if (RemoveByName(black_book, "John")) {
        cout << "removed" << endl;
    } else {
        cout << "not found" << endl;
    }
    if (RemoveByName(black_book, "Tom")) {
        cout << "removed" << endl;
    } else {
        cout << "not found" << endl;
    }
    for (int i = 0; i < black_book.size(); ++i) {
        Friend& f = black_book.at(i);
        cout << f.first_name << " " << f.last_name << " " << f.phone << endl;
    }
    return 0;
}

Output:

removed
not found
Homer Simpson 2064375555
UpAndAdam
  • 4,155
  • 2
  • 25
  • 41
  • Thank you!!! It worked. If you had the time would you be able to give a little explanation about your code, maybe a comment here and there. Regardless thank you very much it helped a lot. – Michael La Oct 21 '15 at 08:19
  • @MichaelLa, for further reading see [Erase-Remove Idiom](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom). – Ralara Oct 21 '15 at 09:14
  • Also, the `if (removed_it == BlackBook.end())` check isn't required. If no entries match the predicate then `remove_if` will return a past-the-end iterator which is the same as 'BlackBook.end()'; therefore the `BlackBook.erase(...` call will do nothing. Unless of course you want to capture that state and return `true`/`false` as this example shows. – Ralara Oct 21 '15 at 09:18
  • @MichaelLa The core part of this code is [remove_if](http://www.cplusplus.com/reference/algorithm/remove_if/). I also use [uniform initialization](https://mbevin.wordpress.com/2012/11/16/uniform-initialization/) and [lambda functions](http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – Андрей Беньковский Oct 21 '15 at 15:58
1

Of course, you can always loop over all Friend elements and delete them manually.

Blackbook::iterator friend = Blackbook.begin();
while (friend != Blackbook.end())
{
    if (friend->last_name == bad_name)
    {
        friend = Blackbook.erase(friend);
    }
    else
    {
        ++friend;
    }
}
SpamBot
  • 1,328
  • 10
  • 24