C++ 创建std::set只复制一个元素,如何修复?

C++ 创建std::set只复制一个元素,如何修复?,c++,set,C++,Set,v_map存储了正确数量的信息,但是当我尝试使用std::set时,它只复制一个元素,我假设是第一个。这是我第一次使用std::set,也许我错过了一些东西…谢谢你的帮助 typedef std::map<std::string,std::pair<int,int>> points_map; void list_average(points_map &v_map) { Comparator compFunctor = [](std::pair<std

v_map存储了正确数量的信息,但是当我尝试使用std::set时,它只复制一个元素,我假设是第一个。这是我第一次使用std::set,也许我错过了一些东西…谢谢你的帮助

typedef std::map<std::string,std::pair<int,int>> points_map;
void list_average(points_map &v_map)
{
    Comparator compFunctor = [](std::pair<std::string,std::pair<int,int>> elem1,std::pair<std::string,std::pair<int,int>> elem2)
            {
                std::pair<int,int> it = elem1.second;
                std::pair<int,int> jt = elem2.second;
                return it.first < jt.first;
            };
    std::set<std::pair<std::string,std::pair<int,int>>,Comparator> v_set(v_map.begin(),v_map.end(),compFunctor);
    for (std::pair<std::string,std::pair<int,int>> it : v_set)
    {
        std::pair<int,int> jt = it.second;
        std::cout << it.first << " " << (jt.second - jt.first) / jt.first<< std::endl;

    }
}
typedef std::map points\u map;
无效列表平均值(点图和v图)
{
比较器compFunctor=[](std::pair elem1,std::pair elem2)
{
std::配对it=elem1.second;
std::对jt=elem2.s;
返回它.first
输出:

foo 1
bar 0
bar 3

现在考虑主版本的第二版:

int main()
{
    points_map v_map = { {"foo", { 1, 2}}, {"bar", { 1, 4}}};

    list_average(v_map);
}
输出:

foo 1
bar 0
bar 3
第二,元素的第一个
都是1,后者取代了第一个。它不是唯一的。这是std::set的缺点


那又怎么样? 不要使用
std::set
,而是使用
std::vector
std::sort
。示例:

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using point_pair = std::pair<int,int>;
using points_map = std::map<std::string, point_pair>;
using string_point_pair = std::pair<std::string, point_pair>;
auto compFunctor = [](string_point_pair const &elem1, string_point_pair const &elem2)
{
    return 
        elem1.second.first != elem2.second.first?
        elem1.second.first < elem2.second.first:
        elem1.second.second < elem2.second.second;
};

void list_average(points_map const &v_map)
{
    std::vector<string_point_pair> v_vec(v_map.begin(),v_map.end());
    std::sort(v_vec.begin(), v_vec.end(), compFunctor);
    for (auto &elem : v_vec)
    {
        const point_pair &jt = elem.second;
        std::cout << elem.first << " " << (jt.second - jt.first) / jt.first<< "\n";

    }
}

int main()
{
    points_map v_map = { {"foo", { 1, 2}}, {"bar", { 1, 4}}, {"baz", { 2, 4}}};

    list_average(v_map);
}

请您出示a。错误可能在代码中的其他地方。是的,但我为其丑陋性提前道歉。原始代码看起来很好。这些天我会在您有
std::pair
的几个地方使用auto。您的比较函数只考虑
std::pair
的三个成员中的一个,而忽略rest.该值对于您尝试插入到集合中的每个对象都是唯一的吗?
std::set
的目的是保持元素的唯一性,而不是对元素进行排序。如果您需要对一组非唯一项进行排序,那么您可能需要一个
std::vector
或另一个序列容器,您可以在其上调用
std::sort
bar 3
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>

using point_pair = std::pair<int,int>;
using points_map = std::map<std::string, point_pair>;
using string_point_pair = std::pair<std::string, point_pair>;
auto compFunctor = [](string_point_pair const &elem1, string_point_pair const &elem2)
{
    return 
        elem1.second.first != elem2.second.first?
        elem1.second.first < elem2.second.first:
        elem1.second.second < elem2.second.second;
};

void list_average(points_map const &v_map)
{
    std::vector<string_point_pair> v_vec(v_map.begin(),v_map.end());
    std::sort(v_vec.begin(), v_vec.end(), compFunctor);
    for (auto &elem : v_vec)
    {
        const point_pair &jt = elem.second;
        std::cout << elem.first << " " << (jt.second - jt.first) / jt.first<< "\n";

    }
}

int main()
{
    points_map v_map = { {"foo", { 1, 2}}, {"bar", { 1, 4}}, {"baz", { 2, 4}}};

    list_average(v_map);
}
foo 1
bar 3
baz 1