本文介绍了无法排序unordered_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类别为ActiveStatusEffect的unordered_set该集合声明如下:

I have an unordered_set of a class ActiveStatusEffect The set is declared as follows:

boost::unordered_set<StatusEffects::ActiveStatusEffect> ActiveStatusEffects;

ActiveStatusEffect定义如下:

ActiveStatusEffect is defined as follows:

class ActiveStatusEffect
    {
    public:
        StatusEffect* effect;
        int ReminaingTurns;
        bool operator<(const ActiveStatusEffect& ase) const
        {
            return *effect < *ase.effect;
        }
        bool operator>(const ActiveStatusEffect& ase) const
        {
            return *effect > *ase.effect;
        }
        bool operator==(const ActiveStatusEffect& ase) const
        {
            return *effect == *ase.effect;
        }
        bool operator!=(const ActiveStatusEffect& ase) const
        {
            return !((*this) == ase);
        }
    };

StatusEffect之间的比较是对分配给状态效果的每个实例的唯一整数之间的比较.

The comparison between StatusEffect's is a comparison between a unique integer assinged to each instance of a status effect.

但是,如果我尝试对效果进行排序,如下所示:

However, if I try to sort the effects like follows:

std::sort(statusSet.begin(), statusSet.end(), [](StatusEffects::ActiveStatusEffect const &se1, StatusEffects::ActiveStatusEffect const &se2){return se1.effect->GetPriority() < se2.effect->GetPriority();});

我在算法头文件中遇到很多错误,例如

I get many errors in the algorithm header file such as

错误199错误C2784: '_Base1 :: difference_type std :: operator -(const std :: _ Revranit< _RanIt,_Base>&,const std :: _ Revranit< _RanIt2,_Base2> &)':无法推断出模板 'const的参数 std :: __ Revranit< _RanIt,_Base>&'从 'boost :: unordered_detail :: hash_const_iterator'c:\ program 文件(x86)\ Microsoft Visual Studio 10.0 \ vc \ include \ algorithm 3806

Error 199 error C2784: '_Base1::difference_type std::operator -(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'boost::unordered_detail::hash_const_iterator' c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm 3806

为什么我不能对集合进行排序?我敢肯定,这与unordered_set有关,因为删除尝试将其排序或更改为向量不会产生错误.

Why am I not able to sort the set? I'm quite certain this is something about unordered_set as removing attempts to sort or changing it to a vector does not generate errors.

推荐答案

boost::unordered_set<Foo> a;
a.insert(...);
...

std::set<Foo> b(a.begin(), a.end());

std::set<Foo> c;
std::copy(a.begin(), a.end(), std::inserter(c, c.end());

Voilà,已排序的集合.

Voilà, a sorted set.

这篇关于无法排序unordered_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:34