这些是我的两个重载函数,用于std::map
容器和std::vector
具有 Lambda 表达式的容器。
有没有人看到一种使它成为模板函数的方法,它可以检查 Lambda 表达式的pair
类型(在我的情况下是对的第二个)和常规范围类型(如vector
,deque
等)。
bool isPlayerIn(vector<Player*> players, int id) {
vector<Player*>::iterator found = find_if(begin(players), end(players),
[id] (Player* player) {
return player->getId() == id;
});
return found != end(players);
}
bool isPlayerIn(map<int, Player*> players, int id) {
map<int, Player*>::iterator found = find_if(begin(players), end(players),
[id] (pair<int, Player*> found) {
return found.second->getId() == id;
});
return found != end(players);
}