C++ 如何对包含地图的列表进行排序

C++ 如何对包含地图的列表进行排序,c++,C++,我试图按照第二个浮点数(在对中)对地图列表进行排序,如下所示: 列表 谁能帮我做这个 到目前为止我已经试过了: 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>>>

我试图按照第二个浮点数(在
对中)对地图列表进行排序,如下所示:

列表

谁能帮我做这个

到目前为止我已经试过了:

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->)
        }
    }
}
void my_排序(列表ans)
{
对于(int i=1;i)
}
}
}

有人能帮我吗…

您不太清楚如何比较两个地图,但一旦您弄明白了,您可以使用自定义小于运算符执行自定义排序,如下所示:

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());
struct less_than_key
{
内联布尔运算符()(映射和映射1、映射和映射2)
{
//在这里,你需要弄清楚是什么让一张地图比另一张地图“小”
//例如,如果特定键的value.second在其他映射中<相同。
返回MAP1小于MAP2;
}
};
排序(yourList.begin()、yourList.end()、less_than_key());

因此,您希望在
std::pair
中按
float
排序,这是
std::map
的一个值,但是如何选择map中的哪一对用于比较呢?这是一个非常复杂的数据结构。你应该重新考虑你的设计。我不明白你想做什么。请显示示例输入数据和所需结果。“按第二个浮点数(成对)列出的贴图列表”。。。严格地说,没有“列表中的第二个浮点数”,如果你对列表进行排序,你实际上是在对地图进行排序。这些地图可能(!)包含成对的浮动。这个比较应该如何进行并不明显。我猜你也要在地图中对这对进行排序?请给我们举个例子。假设你有{{{“a”、{1,3}}、{“b”、{2,6}、{“c”、{3,4}}、{{“a”、{12,10}、{“b”、{0,2}、{“c”、{3,8}}。排序后你想看到什么?@Tony
std::map
总是按键排序(
std::string
)。