我正在使用内置unordered_map<Node*, unordered_set<Edge>>数据结构的c ++编写DiGraph(有向图)类,其中Node和Edge是我自己定义的两个结构。在课堂上,我编写了一种containsNode()方法来搜索图中是否存在a Node。这是containsNode()方法主体:
bool DiGraph::containsNode(const Node * n) const {
auto::const_iterator it = digraph.find(n);
return (it == digraph.end());
}
Run Code Online (Sandbox Code Playgroud)
digraph是DiGraph类型的私有成员unordered_map<Node*, unordered_set<Edge>>。
但是,编译器生成以下错误:
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...
Run Code Online (Sandbox Code Playgroud)
但是,如果我将方法声明为
bool DiGraph::containsNode(Node* n) const {...}(唯一的区别是const从参数列表中删除了关键字),则没有编译错误。
我检查了C ++文档,发现容器中的find()方法声明unordered_map …