Search code examples
c++vectorerase-remove-idiom

How to remove certain specified value from vectors?


mySongs is a vector that store a collection of songs input by the user. In the if statement, the program will check the elements in the vector with user input. If match, it will delete that specified value from the vector. When I look for the solution, I see someone recommend to use remove/erase idiom:. But when I implement in my code, it continue pop up this error C2678 binary '==': no operator found which takes a left - hand operand of type 'Song' (or there is no acceptable conversion)

void deleteSong() {
    string songTitle;

    cout << "\n\t\tPlease enter the particular song name to remove: ";
    cin >> songTitle;

    if (songTitle != "") {
        for (Song songs : mySongs) {
            if (songs.title == songTitle) {
                mySongs.erase(find(mySongs.begin, mySongs.end, songs));  //erase an element with value
                break;
            }
        }
    }
}

Solution

  • This error is due to the fact that the class Song does not have an == operator. This can be solved in one of the two following ways

    1. If you have access to the source code of Song then add the following function to it. please note that const is necessary

       bool operator == ( const Song& song)
      {
           //Do the comparison code here
      }
      
    2. If you don't have access to the source code. Then add the following function

      bool operator == (const Song& song1, const Song& song2)
      {
           // Do the comparison code here  
      }
      

    That said, there is another minor problem with your code

    The erase function should be called like this

         mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));
    

    I decided to add a minimal example to help you. In that example I assumed a certain implementation of Song, but that implementation does not have to be what you have

        #include <iostream>
        #include<vector>
        #include<algorithm>
        #include<string>
        using namespace std;
        class Song
        {
            public:
            std::string title;
        /*  bool operator ==(const Song& other)
            {
                return this->title == other.title;
            }*/
        };
        bool operator ==(const Song& song1,const Song& other)
            {
                return song1.title == other.title;
            }
        std::vector<Song> mySongs={{"ali"},{"ahmed"},{"ali"}};;
        
        void deleteSong() {
            string songTitle;
    
            cout << "\n\t\tPlease enter the particular song name to remove: ";
            cin >> songTitle;
    
            if (songTitle != "") {
                for (const auto& songs : mySongs) {
                    if (songs.title == songTitle) {
            
                        mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));  //erase an element with value
                        break;
                    }
                }
            }
        }
        int main() {
    
            // your code goes here
            return 0;
        }