c++ 尝试实现用于对Map数据类型排序的比较器时是否出现生成错误?

dced5bon  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在尝试为一个Map构建一个比较器,以便与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,
      |               ~~~~~~~^~~~~~~~~

我真的很感激你能提供的任何帮助。

bkhjykvo

bkhjykvo1#

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

相关问题