如何正确使用remove_if?
How to properly Use remove_if?
我正在尝试将remove_if用于数组。该数组包含包含 2 个字符串属性(艺术家和标题(的歌曲对象。我有一个布尔值等于运算符,但在实现方面有问题。下面是我的宋等于运算符:
bool Song::operator==(const Song& s) const
{
return (title_ == s.GetTitle() && artist_ == s.GetArtist()) ? true : false;
}
我还有另一个函数,如果标题或艺术家与传入的参数匹配,它应该删除歌曲。然后返回删除的歌曲数:
unsigned int Playlist::RemoveSongs(const string& title, const string& artist)
{
int startSize = songs_.size();
Song s = Song(title,artist);
// below are some of the things I've attempted from documentation
//songs_.remove_if(std::bind2nd(std::ptr_fun(Song::operator()(s))));
//std::remove_if(songs_.begin(),songs_.end(),s);
int endSize = songs_.size();
return startSize - endSize;
}
尝试使用 lambda... 如下所示(未测试(。 不要忘记使用"[=]"来捕获范围外的变量。
std::remove_if(songs_.begin(),
songs_.end(),
[=](Song &s){return (title == s.GetTitle() && artist == s.GetArtist()) ;})
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 使用函数"remove"删除重复元素
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 链表的泛型函数remove()与成员函数remove)
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- 为什么简单的算术减法在"if"条件下不起作用?