1

I am attempting to use the remove_if for an array. The array contains objects of songs which contain 2 string properties (artist and title). I have a bool equals operator but have issues with implementation. Below is my Song equals operator:

bool Song::operator==(const Song& s) const 
{
    return (title_ == s.GetTitle() && artist_ == s.GetArtist()) ?  true : false;
}

I have another function which is supposed to remove the song if either title or artist match the parameters passed into it. Then returns the number of songs removed:

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;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Potion
  • 785
  • 1
  • 14
  • 36
  • What is `songs_` ? – Jarod42 May 18 '18 at 21:22
  • 2
    `return cond ? true : false;` can just be `return cond;`. – Jarod42 May 18 '18 at 21:23
  • _I have another function which is supposed to remove the songs if either title or artist match_ but your `operator==` only checks if both the title AND artist match. You also haven't really asked a question: presumably there's something wrong with your code, but you haven't explained what is happening. – Tas May 18 '18 at 21:25
  • if you create value, `remove` seems more appropriate than `remove_if` which expects predicate. – Jarod42 May 18 '18 at 21:25
  • oh thanks for the catch on that return statement. songs_ is a list. The Song class contains 2 strings properties for Artist and Title. – Potion May 18 '18 at 21:26
  • As a side note `std::bind2nd` and `std::ptr_fun` are removed in the latest version of the Standard - they shouldn't be used. – DeiDei May 18 '18 at 21:27
  • Related to [listremove-if-using-operator-with-stdfunction](https://stackoverflow.com/questions/50419292/>listremove-if-using-operator-with-stdfunction) – Jarod42 May 18 '18 at 21:27
  • @Tas good point. This is just one of many functions I have which covers AND, OR, XOR or Neither. For this question I am just using a small example. – Potion May 18 '18 at 21:28
  • Are you sure the second condition is what you want? Also, please refrain from completely changing the question after getting an answer. – Deduplicator May 18 '18 at 23:20

1 Answers1

0

Try using lambda... Something like below (not tested). Do not forget to use "[=]" to capture out of scope variables.

std::remove_if(songs_.begin(), 
                   songs_.end(),
                   [=](Song &s){return (title == s.GetTitle() && artist == s.GetArtist()) ;})
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • Okay this would be the correct answer. What can I do in the case of this error since my original structure does not have copy 'object of type 'Song' cannot be assigned because its copy assignment operator is implicitly deleted ' – Potion May 18 '18 at 21:52
  • 2
    Then use `(Song const& s)` for the parameter. – Eljay May 18 '18 at 22:11
  • Do not use const. Just say (Song &s) – Pavan Chandaka May 18 '18 at 23:00