带有条件的c ++排序类向量

时间:2018-11-28 07:45:42

标签: c++

#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <set>

using namespace std;

class Song
{
public:
    Song(string genres, int play, int index);

    string _genres;
    int playTime;
    int priority;
};

Song::Song(string genres,int play,int index)
{
    this->_genres = genres;
    this->playTime = play;
    this->priority = index;
}

template<template <typename> class P = less >
struct compare_pair_second
{
    template<class T1, class T2> bool operator()
        (const pair<T1, T2>&left, const pair<T1, T2>&right) {
        return P<T2>()(left.second, right.second);
    }
};


vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    map<string, int> keyValuePair_TotalPlayTime;
    vector<Song> songList;

    for (int i = 0; i < genres.size(); i++)
    {
        songList.push_back(Song(genres[i], plays[i], i));
    }

    for (int i = 0; i < genres.size(); i++)
    {
        if (keyValuePair_TotalPlayTime.find(genres[i]) == keyValuePair_TotalPlayTime.end())
        {
            keyValuePair_TotalPlayTime.insert(make_pair(genres[i], plays[i]));
        }
        else
        {
            keyValuePair_TotalPlayTime[genres[i]] += plays[i];
        }
    }

    answer.resize(keyValuePair_TotalPlayTime.size() * 2);

    vector<pair<string, int>> descendingTotalPlayList(keyValuePair_TotalPlayTime.begin(), keyValuePair_TotalPlayTime.end());
    sort(descendingTotalPlayList.begin(), descendingTotalPlayList.end(), compare_pair_second<greater>());

//what can i do?


    return answer;
}

int main()
{
    vector<string> testGenres{ "classic", "pop", "classic", "classic", "pop","blad" };
    vector<int> testPlays{ 500, 600, 150, 800, 2500,600 };

    solution(testGenres, testPlays);
}

我有这个代码。我想用以下三个条件对songList向量进行排序。

第一个条件:_genres首先应排序。 并且playTime应该根据第一个条件以降序排序。优先级也应在先前条件的控制下进行排序。

那么,我该如何在备注中编写代码?

(对不起,我的英语不好,请理解我的写作能力)

0 个答案:

没有答案