我在嘗試從矢量中擦除字符串時看到一些大的錯誤消息。我試圖使用擦除刪除成語,但它不工作。這裏有一些片段來展示我的困境。如何從矢量中刪除具有給定名稱的玩家
team.players.erase(remove(team.players.begin(), team.players.end(),
player_name), team.players.end());
team.players在該結構中聲明:
struct team
{
string name;
player captain;
vector<player> players;
};
載體team.players被動態地填充在程序運行時,並且在某個點,用戶具有通過去除一個玩家的選擇一份菜單。
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);
但是我得到這些錯誤,因爲我試圖編譯我的程序(鐺++),該函數被調用之前很久。這裏是錯誤:
/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.
任何想法我可以解決這個問題?
請顯示[MCVE。 – user463035818
如何創建一個比較'player'對象和'std :: string'的'operator =='函數?因爲這是編譯器告訴你的問題,所以你試圖比較'player'對象和'std :: string'。 –
您的載體包含'player's所以你不能從它刪除一個字符串:P – user463035818