C ++ STL映射在地图中未正确插入的地图中

时间:2017-08-31 23:14:50

标签: stl maps

我正在制作一个程序,使用地图来保存和组织文本文件中的音乐库。艺术家地图持有包含歌曲地图的专辑地图。我遇到的问题是,每位艺术家的每张专辑都插在每位艺术家的下方,而不仅仅是该艺术家的专辑。我已将其缩小到此功能,以确定艺术家是否是新的并将其插入艺术家地图中(如果是)。一旦相册被理顺,相册和歌曲也是如此。

这是我的班级信息

class Song {
public:

    string title;
    int time;
    int track;
};

class Album {
public:
    map <int, Song *> songs;
    string name;
    int time;
};

class Artist {
public:
    map <string, Album *> albums;
    string name;
    int time;
    int nsongs;
};

以下是我认为问题所在的功能:

map <string, Artist*> newArtist(Artist* a, Album* al, Song* s, string artist, string album, int track, map <string, Artist*> lib){
    if (lib.find(artist) == lib.end()){
        cout << "New Artist: " << artist << endl;
        a->name = artist;
        lib[artist] = a;
    } else if (lib.find(artist) != lib.end()){
        cout << "Old Artist: " << artist << endl;
    }
    if (lib.find(artist)->second->albums.find(album) == lib.find(artist)->second->albums.end()){
        cout << "New Album: " << album << endl;
        al->name = album;
        lib.find(artist)->second->albums[album] = al;
    } else if (lib.find(artist)->second->albums.find(album) != lib.find(artist)->second->albums.end()){
        cout << "Old Album: " << album << endl;
    }
    return lib;
}

打印出来的是什么。正如你所看到的,每张专辑都在每一位艺术家之下。

King Crimson
        A Russian Piano Recital
        Intergalactic Boogie Express
        Jazz Piano III (A Smithsonian Collection)
        Josef Hofmann & Ignace Jan Paderewski Play Liszt
        Larks' Tongues In Aspic
        Paderewski Plays Concert No. 1
        Three of a Perfect Pair
Larkins, Ellis
        A Russian Piano Recital
        Intergalactic Boogie Express
        Jazz Piano III (A Smithsonian Collection)
        Josef Hofmann & Ignace Jan Paderewski Play Liszt
        Larks' Tongues In Aspic
        Paderewski Plays Concert No. 1
        Three of a Perfect Pair
League of Crafty Guitarists
        A Russian Piano Recital
        Intergalactic Boogie Express
        Jazz Piano III (A Smithsonian Collection)
        Josef Hofmann & Ignace Jan Paderewski Play Liszt
        Larks' Tongues In Aspic
        Paderewski Plays Concert No. 1
        Three of a Perfect Pair
Lewin, Michael
        A Russian Piano Recital
        Intergalactic Boogie Express
        Jazz Piano III (A Smithsonian Collection)
        Josef Hofmann & Ignace Jan Paderewski Play Liszt
        Larks' Tongues In Aspic
        Paderewski Plays Concert No. 1
        Three of a Perfect Pair
Paderewski, Ignace
        A Russian Piano Recital
        Intergalactic Boogie Express
        Jazz Piano III (A Smithsonian Collection)
        Josef Hofmann & Ignace Jan Paderewski Play Liszt
        Larks' Tongues In Aspic
        Paderewski Plays Concert No. 1
        Three of a Perfect Pair

关于我在这里做错了什么的想法。

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我需要在主程序循环中创建对象,我不得不删除删除它们的语句。