In one of my code std::map::find() is crashing the programm.
actually I am using map<ConstructPointer, QStringList*>;
My ConstructPointer contains and constructed by two data members of integer type (Enums), and it also have operator==() overload.
I've my own LessFunctor for Compare.
Code:
typedef
std::map<ConstructPointer, QStringList*, ConstructPointer::FakeLessFunctor>
ConstructMap;
ConstructMap constructList;
Code:
class ConstructPointer{
private:
Construct c;
ConstructType t;
public:
Construct construct(){return c;}
ConstructType constructType(){return t;}
public:
ConstructPointer(Construct cns, ConstructType cnsT):c(cns), t(cnsT){}
bool operator==(const ConstructPointer& cnsPt) const{
return (cnsPt.c == c && cnsPt.t == t);
}
/*
friend bool operator==(const ConstructPointer& lCnsPt, const ConstructPointer& rCnsPt){
return (lCnsPt.c == rCnsPt.c && lCnsPt.t == rCnsPt.t);
}
*/
public:
struct LessFunction{
bool operator()(const ConstructPointer& lhsCnsPt, const ConstructPointer& rhsCnsPt) const{
return lhsCnsPt.t < rhsCnsPt.t;
}
};
struct FakeLessFunctor{
bool operator()(const ConstructPointer&, const ConstructPointer&) const{return true;}
};
};
In my code I am executing constructList.find() in the following way
Code:
ConstructPointer cnsPt(cns, cnsTp);//Constructing a new Object
ConstructMap::const_iterator it = constructList.find(cnsPt);
My program is crashing on the line where I am executing find().
____________________
MY INVESTIGATION
````````````````````
To investigate I looked at the STL source. where I found std::map executes find() method of Tree and from that stl_tree.h I found
Code:
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc>
typename _Rb_tree<_Key, _Val, _KeyOfValue,
_Compare, _Alloc>::const_iterator
_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
find(const _Key& __k) const
{
const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
return (__j == end()
|| _M_impl._M_key_compare(__k,
_S_key(__j._M_node))) ? end() : __j;
}
So I saw
Code:
_Rb_tree_impl<_Compare> _M_impl;
Code:
template<typename _Key_compare,
bool _Is_pod_comparator = __is_pod(_Key_compare)>
struct _Rb_tree_impl : public _Node_allocator
{
_Key_compare _M_key_compare;
and this is the _Rb_tree class template declaration
Code:
template<typename _Key, typename _Val, typename _KeyOfValue,
typename _Compare, typename _Alloc = allocator<_Val> >
class _Rb_tree{
which means
Code:
_M_key_compare(__k,_S_key(__j._M_node))
will invoke the lessFunction's operator()() which returns bool type if lhs < rhs. But how will understand by LessFunctor::operator()() wheather its equal or not