I have a std::vector<std::pair<int,std::pair<Bone,std::string> > >
I’m trying to sort it with std sort, with the expectation that it is sorted by int, but instead I get 21 errors related to the sort call.
What could be wrong?
Code:
std::vector<std::pair<int,std::pair<Bone,std::string> > > tempBones;
std::sort(tempBones.begin(),tempBones.end());
Errors:
Error 1 error C2784:
'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)': could not deduce template argument for'const std::basic_string<_Elem,_Traits,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 2 error C2784:
'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for'const _Elem *'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 3 error C2784:
'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for'const std::basic_string<_Elem,_Traits,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 4 error C2784:
'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)': could not deduce template argument for'const std::_Tree<_Traits> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 5 error C2784:
'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)': could not deduce template argument for'const std::vector<_Ty,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 6 error C2784:
'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)': could not deduce template argument for'const std::list<_Ty,_Ax> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 7 error C2784:
'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for'const std::reverse_iterator<_RanIt> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 8 error C2784:
'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)': could not deduce template argument for'const std::_Revranit<_RanIt,_Base> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 9 error C2784:
'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for'const std::pair<_Ty1,_Ty2> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 102Error 10 error C2676: binary
'<':'const skl::Bone'does not define this operator or a conversion to a type acceptable to the predefined operator c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 10Error 11 error C2784:
'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)': could not deduce template argument for'const std::basic_string<_Elem,_Traits,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 12 error C2784:
'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for'const _Elem *'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 13 error C2784:
'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)': could not deduce template argument for'const std::basic_string<_Elem,_Traits,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 14 error C2784:
'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)': could not deduce template argument for'const std::_Tree<_Traits> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 15 error C2784:
'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)': could not deduce template argument for'const std::vector<_Ty,_Alloc> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 16 error C2784:
'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)': could not deduce template argument for'const std::list<_Ty,_Ax> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 17 error C2784:
'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)': could not deduce template argument for'const std::reverse_iterator<_RanIt> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 18 error C2784:
'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)': could not deduce template argument for'const std::_Revranit<_RanIt,_Base> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 19 error C2784:
'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for'const std::pair<_Ty1,_Ty2> &'from'const skl::Bone'c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103Error 20 error C2676: binary
'<':'const skl::Bone'does not define this operator or a conversion to a type acceptable to the predefined operator c:\Program Files\Microsoft Visual Studio 9.0\VC\include\utility 103
Why would you expect it to be sorted by
int? Anycontainer<T>is sorted byT, and sorting requires that there be a less-than comparison forT. Now forpair<S,T>, there is anoperator<, defined as lexicographic ordering, but this requires comparability of bothSandT:Moving down recursively, you need
Boneto be less-than comparable. Do check, any bet that you didn’t implement a comparison operator for that type!Edit: According to your update that is precisely your problem.
Remark: You might wonder why lexicographic compare is the default: It’s the only sensible comparison for which
!(x < y) && !(y < x)is identical tox == y, provided the same holds for both sub-types individually.Update: If you only want ordering by the first member, you should write a custom comparator predicate, or use a multi-map instead.
Custom comparator:
Note that
sortmay rearrange elements with the same key. If you want those to maintain their relative ordering, usestable_sortinstead.Multimap:
The multimap is a more complicated structure than the vector, and its elements are always kept in sorted order by key value, i.e. your
int. Which one is preferable depends on what you’re doing.