0

我已经编写了一个排序类来对多图进行排序,但是当我将元素插入到地图中时,会出现以下编译器错误:

1>c:\program files\microsoft visual studio 9.0\vc\include\xutility(313) : error C2664: 'bool MapSort::operator ()(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'const std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &'

有人可以帮忙吗?

class MapSort
{
    public:
        MapSort();
        ~MapSort();

        public:
            bool operator() ( pair<T,T>& i, pair<T,T>& j) 
            {
                return i.first.GetID() < j.first.GetID();

            }

};


multimap < pair < T,T >,P > CurrMap;
CurrMap.insert( multimap < pair < T, T >,Metric >::value_type(make_pair< T,T >(aAttractionA,aAttractionB),CurrP))
//
4

3 回答 3

1
pair<T,T>& i  and pair<T,T>& j

应该

pair<T,T> const& i and pair<T,T> const& j

(参考)

于 2011-06-20T09:07:43.520 回答
0

你忘了const-正确性:

bool operator()(pair<T,T> const& i, pair<T,T> const& j) const
{
    return i.first.GetID() < j.first.GetID();
}
  • 此函数中的任何内容都不需要是可变的,所以“为什么不”使函数及其输入const

  • 事实上,这是容器合同所期望的。

此外,我不确定您自己的排序在这里是否有效或是否有意义。它当然看起来并不完全是有目的的。pair<T,T>已经很自然地排序了:如果有的话,你可能有一个bool operator<适合你的 type T

于 2011-06-20T09:05:30.330 回答
0

您的排序谓词必须const引用。

另外,多图不是已经排序了吗?

于 2011-06-20T09:05:35.650 回答