c++ 如何用类的成员搜索对象指针的向量?

7qhs6swi  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(115)

我正在为一门课设计这个程序。我需要模拟一个播放列表,可以在音乐库中添加和删除歌曲,在播放列表中添加和删除歌曲,等等。
我有一个Song类和一个Playlist类,一个vector<Song*>库(用于歌曲库),以及一个包含vector<Song*> songList的vector<Playlist>播放列表。如果我想从库中删除一首歌曲,并且在库向量中获得了一个歌曲指针的索引,我将如何检查该“歌曲”是否在任何“播放列表”中?以下是我拥有的相关代码部分:

using namespace std;

    class Song {
    public:
         Song(string title, string line, int count) {
              name = title;
              firstLine = line;
              playCount = count;
         }
         string GetName() {
              return name;
         }

         string GetFirstLine() {
              return firstLine;
         }
    private:
         string name;
         string firstLine;
         int playCount;;
    };

    class Playlist {
    public:
         Playlist(string name = "none"){
              pName = name;
         }
         void AddSong(Song* song) {
              songPtr.push_back(song);
         }
         void RemoveSong(int songIndex) {
              songPtr.erase(songPtr.begin() + songIndex);
         }
         string GetSongAtIndex(int index) {
              return (songPtr.at(index))->GetName();
         }

    private:
         string pName;
         vector<Song*> songPtr;
    };

    int main() {
    string userIn;
    vector<Song*> library;
    vector<Playlist> playlists;

    // code for adding songs to library using "new"
    // code for adding songs to playlist using "new"
    

    // delete song from library
    // first, list songs in library
        size = library.size();
        for (i = 0; i < size; i++) {
            cout << "  " << i << ": " << (library.at(i))->GetName();
            cout << endl;
        }
        // have user pick which song they want erased
        cout << "Pick a song index number: ";
        cin.clear();
        cin >> sIndex;

        // FIXME: check if song is in any playlist, and if it is, remove it
        string songName = (library.at(sIndex))->GetName();
        int size = playlists.size(); //remove song from playlists
        for (i = 0; i < size; i++) {
            int size2 = (playlists.at(i)).GetPSize();
            for (j = 0; j < size2; j++) {
                string tempName = (playlists.at(j)).GetSongAtIndex(j);
                if (tempName == songName) {
                    (playlists.at(i)).RemoveSong(j);
                }
            }
        }
        // code to delete song from library and free the memory

我有什么不工作,当我试图列出歌曲库中的歌曲后,我删除它,它给了我一个“超出范围”的错误和崩溃的程序。当我注解出有关在播放列表中找到它的部分,它从库中删除就好了。只有当我试图检查这首歌是否在播放列表中时,我才得到错误。我是相对较新的编程,所以很可能我只是错过了一些东西,但我不知道如何解决这个问题。

eimct9ow

eimct9ow1#

如果从播放列表中删除元素,则不应增加索引,而应减小大小:

// check if song is in any playlist, and if it is, remove it
string songName = library.at(sIndex)->GetName();
int size = playlists.size(); //remove song from playlists
for (j = 0; j < size2; ) {                // do not increment j here
    string tempName = playlists.at(i).GetSongAtIndex(j); // i, not j for playlists.at()
    if (tempName == songName) {
        playlists.at(i).RemoveSong(j);  // j remains the same
        --size2;                          // one element less
    } else {
        ++j;                              // increment here
    }
}

相关问题