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

How to erase a player with a given name from a vector


I'm seeing some big bad error messages whilst trying to erase a string from a vector. I'm trying to use the erase-remove idiom but it's not working. Here are some snippets to show my predicament.

team.players.erase( remove(team.players.begin(), team.players.end(),
                            player_name), team.players.end());

team.players is declared in this struct:

struct team 
{
    string name;
    player captain;
    vector<player> players;

};

The vector team.players is populated dynamically as the program runs, and at some point a user has the option to remove a player via a menu.

if (select_option == PRINT_TEAM) print_team(team);
    if (select_option == CHANGE_TEAM_NAME) change_team_name(team);
    if (select_option == CHANGE_CAPTAIN) change_captain(team);
    if (select_option == ADD_PLAYER) add_player(team);
    if (select_option == REMOVE_PLAYER) remove_player(team);
    if (select_option == QUIT) exit(0);

However I'm getting these errors as I tried to compile my program (clang++), long before the function is called. Here are the errors:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:865:22: error:      invalid operands to binary expression ('player' and 'const      std::__1::basic_string<char>')
        if (*__first == __value_)
            ~~~~~~~~ ^  ~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2125:22: note:
      in instantiation of function template specialization
      'std::__1::find<std::__1::__wrap_iter<player *>, std::__1::basic_string<char> >'
      requested here
    __first = _VSTD::find(__first, __last, __value_);
                     ^
program2.cpp:172:25: note: in instantiation of function template specialization
      'std::__1::remove<std::__1::__wrap_iter<player *>, std::__1::basic_string<char>
      >' requested here
    team.players.erase( remove(team.players.begin(), team.players.end(),
                        ^
2 errors generated.

Any idea how I can fix this?


Solution

  • The error message is quite cryptic, especially for beginners, but it seems that the compiler is unable to find out an operator== implementation to compare instances of your player class with std::string objects.

    Maybe what you really want is using std::remove_if, specifying a custom condition in which you compare the player's name with the std::string storing the name to remove.

    You can express this custom condition using a lambda, e.g. :

    team.players.erase( 
        std::remove_if(
            team.players.begin(), 
            team.players.end(),
    
            [&player_name](const player& p) {
                return p.name == player_name;
            }),
        team.players.end()
    );