The Problem
In the code below, I have found that I can insert into a set<> using a custom sorting criterion, but I cannot test for the presence of an element using set<>::find(). The following compilation error results upon a call to set<>::find():
error: no matching function for call to ‘std::setstd::shared_ptr<node_t, node_comp_t>::find(char&) const’
return children.find(c) != children.end();
This is confusing to me since element equivalence is defined by:
!(lhs < rhs) && !(rhs < lhs)
As can be seen below, I have the required operator defined.
Undesirable Alternative
As an alternative, I can search through the set<> element-by-element, but that is not preferable since doing so would run in linear time.
My Questions
- What am I misunderstanding?
- Is there a way I can search in logarithmic time, as one normally could with set<>::find()?
Code
struct node_t;
using node_sp_t = shared_ptr<node_t>;
class node_comp_t
{
public:
inline bool operator()(const node_sp_t &lhs, const node_sp_t &rhs) const;
};
using node_sp_set_t = set<node_sp_t, node_comp_t>;
class node_t
{
public:
explicit node_t(char c_p): c{c_p} {}
inline char get_c() const
{
return c;
}
void add_child(char c)
{
if (!child_exists(c))
children.insert(make_shared<node_t>(c));
}
bool child_exists(char c) const
{
// c is not of the children set element type (node_sp_t).
// So, find() cannot be used with c.
//
// Why does node_comp_t not get used by find()?
//
// How may I search for c in logarithmic time if not by using find()?
//
return children.find(c) != children.end();
// This works, but it runs in linear time!
// for (const node_sp_t &node_sp : children)
// {
// if (node_sp->get_c() == c)
// return true;
// }
// return false;
}
private:
char c;
node_sp_set_t children;
};
inline bool node_comp_t::operator()(const node_sp_t &lhs, const node_sp_t &rhs) const
{
return lhs->get_c() < rhs->get_c();
}
Aucun commentaire:
Enregistrer un commentaire