I am writing a DiGraph (directed graph) class with the c++ built in unordered_map<Node*, unordered_set<Edge>>
data structure, where Node and Edge are two structs I defined myself. And in the class I wrote a containsNode()
method to search if a Node
is in the graph. This is the containsNode()
method body:
bool DiGraph::containsNode(const Node * n) const {
auto::const_iterator it = digraph.find(n);
return (it == digraph.end());
}
digraph
is the private member of DiGraph of type unordered_map<Node*, unordered_set<Edge>>
.
However, the compiler generates the following error:
error: no matching member function for call to 'find'
auto::const_iterator it = digraph.find(n);
candidate function not viable: 1st argument ('const Node *') would lose const qualifier
const_iterator find(const key_type& __k) const {return __t...
However, if I declare the method as
bool DiGraph::containsNode(Node* n) const {...}
(the only difference being that the const
keyword removed from the argument list) then there is no compilation error.
I checked the C++ documentation and saw that the find()
method declaration in the unordered_map
container has the const
keyword:
std::unordered_map::find
const_iterator find(const Key& key) const;
Therefore I think there shouldn't be a compilation error, so why do I get one?