Search code examples
c++c++11vectoreraseerase-remove-idiom

A vector holds class objects, The class object contains 3 strings per object. How do i find the specific string, then delete the entire element?


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!


Solution

  • 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