如何对其中包含地图的列表进行排序

how to sort a list containing maps in it

本文关键字:列表 排序 地图 包含      更新时间:2023-10-16

我正在尝试按照其第二浮标编号(在pair<float,float>中(对地图进行排序:

list<map<string, pair<float, float>>>

任何人都可以帮助我...

我已经尝试了此操作:

void my_sort(list<map<string, pair<float, float>>> ans)
{
    for (int i = 1; i < ans.size(); i++)
    {
        for (list<map<string, pair<float, float>>>::iterator j = ans.begin(); j < ans.end; j++)
        {
            if(j->)
        }
    }
}

任何人都可以帮助我...

您不太清楚如何比较两个地图,但是一旦您弄清楚您可以使用少于操作员进行自定义排序执行自定义排序,例如:

struct less_than_key
{
    inline bool operator() (map<string, pair<float, float>> &map1, map<string, pair<float, float>> &map2)
    {
        // Here you need to figure out what makes one map 'less than' the other
        // If if a specific key's value.second is < the same in the other map, for example.
        return map1IsLessThanMap2;
    }
};
std::sort(yourList.begin(), yourList.end(), less_than_key());