尝试实现用于对地图数据类型进行排序的比较器时出现构建错误?

Getting build error when trying to implement a comparator for sorting map data type?

提问人:siva_uchiha 提问时间:12/29/2022 更新时间:12/29/2022 访问量:60

问:

我正在尝试为与 sort() 函数一起使用的地图构建一个比较器。 考虑一个名为 right 的 map 对象,其 ID 和 coordinates 作为基本成员。我正在尝试用欧几里得距离对这个物体的元素进行排序。下面是代码。

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
namespace NONBCG_DATA_ALGO
{
    template<typename T>
    T distance(std::vector<T> P1, std::vector<T> P2 , int dim)
    {
        if ((typeid(T) == typeid(int)) || (typeid(T) == typeid(double)) || (typeid(T) == typeid(float)) )
        {
            float accum = 0;
            for(int i=0; i<dim; i++)
            {
                accum += pow((P2[i]-P1[i]),2);

            }
            return sqrt(accum);
        }
        else
        {
            throw std::invalid_argument("Type should be either int,double or float");
        }
        

    }
    
    template<typename T>
    class distance_compare_asc_comp_id_2D
    {
        public:
        distance_compare_asc_comp_id_2D(std::vector<T> ipt):Pt(ipt){};
        bool operator()(const std::pair<int,std::vector<T>>& p1,const std::pair<int,std::vector<T>>&p2)
        {
            
            if ((typeid(T) == typeid(int)) || (typeid(T) == typeid(double)) || (typeid(T) == typeid(float)) )
            {
                
                return NONBCG_DATA_ALGO::distance<T>(Pt,p1.second,2) < NONBCG_DATA_ALGO::distance(Pt,p2.second,2);

            }
            else
            {
                throw std::invalid_argument("Type should be either int,double or float");
            }

        }
        private:
        std::vector<T> Pt;
    };


};

int main() {
    // Write C++ code here
   std::map<int,std::vector<double>> right;
   right.insert(std::pair<int,std::vector<double>>(1,{2,8,3}));
   right.insert(std::pair<int,std::vector<double>>(6,{2.5,5.4,3}));
   sort(right.begin(),right.end(),NONBCG_DATA_ALGO::distance_compare_asc_comp_id_2D<double>(std::vector<double>{0.0,0.0}));

    return 0;
}

我在构建时收到以下错误

In file included from /usr/include/c++/9/algorithm:62,
                 from /tmp/wxeRdlKRUn.cpp:6:
/usr/include/c++/9/bits/stl_algo.h: In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_Rb_tree_iterator<std::pair<const int, std::vector<double> > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<NONBCG_DATA_ALGO::distance_compare_asc_comp_id_2D<double> >]':
/usr/include/c++/9/bits/stl_algo.h:4899:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::_Rb_tree_iterator<std::pair<const int, std::vector<double> > >; _Compare = NONBCG_DATA_ALGO::distance_compare_asc_comp_id_2D<double>]'
/tmp/wxeRdlKRUn.cpp:70:122:   required from here
/usr/include/c++/9/bits/stl_algo.h:1968:22: error: no match for 'operator-' (operand types are 'std::_Rb_tree_iterator<std::pair<const int, std::vector<double> > >' and 'std::_Rb_tree_iterator<std::pair<const int, std::vector<double> > >')
 1968 |     std::__lg(__last - __first) * 2,
      |               ~~~~~~~^~~~~~~~~

我非常感谢您能提供的任何帮助。

C++ 模板 STL

评论

2赞 Borgleader 12/29/2022
问题不在于比较器本身,问题在于您正在尝试对地图进行排序。你不能那样做。地图的键是不可变的,尝试排序只会失败。
2赞 Borgleader 12/29/2022
if ((typeid(T) == typeid(int)) || (typeid(T) == typeid(double)) || (typeid(T) == typeid(float)) )这整行可以而且应该是一个静态断言。当您可以在编译时捕获此异常时,则无需运行时异常。
1赞 molbdnilo 12/29/2022
A 按其键排序,不能重新排序。也许(键、值)对的向量会更合适?std::map
1赞 john 12/29/2022
这里有几点错误 1) 你不能对地图进行排序,而是在创建地图时将比较器类型作为第三个模板参数传递,然后根据比较器自动对地图进行排序 2) 比较器在映射的键上工作,而不是在键/值对上工作,3) 在比较器中必须声明operator()const
0赞 siva_uchiha 12/29/2022
(约翰,博格利德,莫尔布德尼洛)感谢您的建议,将尝试在创建过程中将比较器作为模板的第三个参数传递,或者将其转换为向量以执行该过程。

答:

0赞 siva_uchiha 12/29/2022 #1

MolbdNilo 关于使用对向量的建议非常适合我的用法。谢谢大家的帮助!