const_iterator:嵌套类还是友语类?

const_iterator: nested class or friend class?

本文关键字:友语 嵌套 iterator const      更新时间:2023-10-16

我在这个SparseGraph类中const_iterator遇到一些严重的问题

SparseGraph 类:

出于本文的意图和目的,可以将SparseGraph视为边的列表(或向量向量)列表(这通常称为邻接列表)。然后,const_iterator将循环访问列表列表中的每条边。

考虑:

template <typename NodeType, typename EdgeType>
class SparseGraph
{
public:
//...
using EdgeList = std::vector<EdgeType>;
using AdjacencyList = std::vector<EdgeList>;
//...
private:
//...
AdjacencyList m_adj_list;
//...
}

初始尝试const_iterator(嵌套类)

//----SparseGraph
template <typename NodeType, typename EdgeType>
class SparseGraph
{
public:
class const_iterator; //nested
//...
}
//----SparseGraph::const_iterator
template <typename NodeType, typename EdgeType>
class SparseGraph<NodeType, EdgeType>::const_iterator
{
private:
using NodeIterator = typename SparseGraph<NodeType, EdgeType>::AdjacencyList::const_iterator;
using EdgeIterator = typename SparseGraph<NodeType, EdgeType>::EdgeList::const_iterator;
//this is used to iterate through each edge list corresponding to each node
NodeIterator m_node_it;
//this is used to iterate through each edge in each edge list.
EdgeIterator m_edge_it;
public:
const_iterator(const NodeIterator& node_it, const EdgeIterator& edge_it)
: m_node_it{node_it}, m_edge_it{edge_it} {}
const_iterator& operator++(int)
{
//TODO
return *this;
}
//...

正如人们可能注意到的那样,operator++()给了我"非静态引用"错误:

错误:无效使用非静态数据成员"稀疏图::m_adj_list">

从:

//...
if(m_node_it == m_adj_list.end()) { //<--here,
//loop back around to the beginning
m_node_it = m_adj_list.begin(); //<--here,
}
m_edge_it = m_node_it.begin();//<--and here.
//...

初次尝试const_iterator(朋友班)

现在我们有一些类似的东西:

//----SparseGraph
template <typename NodeType, typename EdgeType>
class SparseGraph
{
public:
friend class const_iterator; //friend
//as before...
}
//----SparseGraph::const_iterator-------------------------------------------------------------------------------------------------------------
template <typename NodeType, typename EdgeType>
class const_iterator
{
private:
using NodeIterator = typename SparseGraph<NodeType, EdgeType>::AdjacencyList::const_iterator;
using EdgeIterator = typename SparseGraph<NodeType, EdgeType>::EdgeList::const_iterator;
const SparseGraph<NodeType, EdgeType>& m_graph;
NodeIterator m_node_it;
EdgeIterator m_edge_it;
public:
const_iterator(const SparseGraph<NodeType, EdgeType>& graph,
const NodeIterator& node_it,
const EdgeIterator& edge_it)
: m_graph{graph} {}
//...

然后给了我错误

错误:"const_iterator"未命名类型

template <typename NodeType, typename EdgeType>
class SparseGraph
{
//...
public:
//...
const_iterator begin() const;
//...
}

错误:在"稀疏图::const_iterator"之前需要"类型名",因为"稀疏图"是依赖范围

template <typename NodeType, typename EdgeType> typename 
SparseGraph<NodeType, EdgeType>::const_iterator SparseGraph<NodeType, EdgeType>::begin() const
{
return const_iterator(*this, m_adj_list.begin(), m_adj_list.begin() >begin());

然后更改为

错误:没有在类"SparseGraph"中声明的"typename SparseGraph::const_iterator SparseGraph::begin() const"成员函数

当我添加该类型名称时。我怀疑如果我解决了第一个错误,这将得到解决,但是我不知道那里有什么问题。此外,友元类现在正在污染包含它的任何翻译单元的范围。

我当前的代码

#ifndef SPARSE_GRAPH_H
#define SPARSE_GRAPH_H
#include <iostream>
#include <vector>
enum
{
invalid_node_index=-1
};
template <typename NodeType, typename EdgeType>
class SparseGraph
{
public:
class const_iterator;
using Node = NodeType;
using Edge = EdgeType;
using NodeList = std::vector<Node>;
using EdgeList = std::vector<Edge>;
using AdjacencyList = std::vector<EdgeList>;
private:
//...
//Similarly to the NodeList, each node index is analogous
//to its index in this list. Each element of this list contains
//a sub-list of edges associated with a particular node.
//For example, the list at index 3 will contain all edges associated
//with the node with an index of 3. This is so we can have an O(1) lookup time.
AdjacencyList m_adj_list;
//...
public:
SparseGraph() {}
//...
const_iterator begin() const;
const_iterator end() const;
//...
};





//----SparseGraph::const_iterator-------------------------------------------
template <typename NodeType, typename EdgeType>
class const_iterator
{
private:
using NodeIterator = typename SparseGraph<NodeType, EdgeType>::AdjacencyList::const_iterator;
using EdgeIterator = typename SparseGraph<NodeType, EdgeType>::EdgeList::const_iterator;
NodeIterator m_node_it;
EdgeIterator m_edge_it;
const NodeIterator m_begin;
const NodeIterator m_end;
public:
const_iterator(NodeIterator node_it,
EdgeIterator edge_it,
NodeIterator begin,
NodeIterator end)
: m_node_it{node_it}, m_edge_it{edge_it}, m_begin{begin}, m_end{end} {}
const_iterator& operator++(int)
{
//are we at the end of a valid edge list?
if(m_node_it != end && m_edge_it == m_node_it->end()) {
//move to the next non-empty edge list or to the end of the adjacency list
while(m_node_it != end && m_node_it->empty()) {
m_node_it++;
}
if(m_node_it != end) {
m_edge_it = m_node_it->begin();
}
}
else {
m_edge_it++;
}
return *this;
}
const_iterator& operator--(int)
{
//TODO
return *this;
}
const typename SparseGraph<NodeType, EdgeType>::Edge& operator*() const
{
return *m_edge_it;
}
const typename SparseGraph<NodeType, EdgeType>::Edge* operator->() const
{
return &(*m_edge_it);
}
bool operator==(const const_iterator& other) const
{
return m_node_it == other.m_node_it &&
m_edge_it == other.m_edge_it;
}
bool operator!=(const const_iterator& other) const
{
return !(*this == other);
}
};





//----SPARSE_GRAPH----------------------------------------------------------
//----PUBLIC FUNCTIONS------------------------------------------------------
//----begin()
template <typename NodeType, typename EdgeType>
typename SparseGraph<NodeType, EdgeType>::const_iterator SparseGraph<NodeType, EdgeType>::begin() const
{
return const_iterator(m_adj_list.begin(),
m_adj_list.begin()->begin(),
m_adj_list.begin(),
m_adj_list.back().end());
}
//----end()
template <typename NodeType, typename EdgeType>
typename SparseGraph<NodeType, EdgeType>::const_iterator SparseGraph<NodeType, EdgeType>::end() const
{
return const_iterator(m_adj_list.end(),
m_adj_list.end()->end(),
m_adj_list.begin(),
m_adj_list.back().end());
}
#endif // SPARSE_GRAPH_H

我的问题是,我应该怎么做?我应该使用嵌套的const_iterator类还是const_iterator类的朋友,或者哪种方式是标准的?我应该如何实施const_iterator以避免这些问题?总的来说,我正在寻找一个双向const_iterator,它将遍历邻接列表(列表列表)中的每个边缘,而不允许更改所述邻接列表。

我看过这个网站上与"const_pointer实现"相关的其他帖子,甚至提到了Stroustrup的"C++编程语言",但到目前为止,还没有任何东西让我深入了解如何正确实现与此类似的const_iterator。

我对如何正确执行此操作完全不满意,因此对任何事情(可能与手头的主要问题无关)的任何帮助将不胜感激。

您的原版

错误:无效使用非静态数据成员"稀疏图::m_adj_list">

表示范围内没有获取m_adj_list.begin()SparseGraph。你不需要调用它,只需将 SparseGraph::m_adj_list 的开始和结束存储在您的const_iterator

template <typename NodeType, typename EdgeType>
class SparseGraph<NodeType, EdgeType>::const_iterator
{
private:
using NodeIterator = typename SparseGraph<NodeType, EdgeType>::AdjacencyList::const_iterator;
using EdgeIterator = typename SparseGraph<NodeType, EdgeType>::AdjacencyList::const_iterator;
const_iterator(NodeIterator node_it, EdgeIterator edge_it, NodeIterator adjacent_begin, NodeIterator adjacent_end) : m_node_it{node_it}, m_edge_it{edge_it}, m_adjacent_begin{adjacent_begin}, m_adjacent_end{adjacent_end} {}
const NodeIterator m_adjacent_begin;
const NodeIterator m_adjacent_end;
NodeIterator m_node_it;
EdgeIterator m_edge_it;
const_iterator& operator++(int)
{
//are we at the end of this edge list?
if(m_edge_it == m_node_it->end()) {
//move to the next node's edge list.
m_node_it++;
//are we past the last node's edge list?
if(m_node_it == m_adjacent_end) {
//loop back around to the beginning
m_node_it = m_adjacent_begin;
}
m_edge_it = m_node_it.begin();
}
else {
m_edge_it++;
}
return *this;
}
}

通过将友元类转换为嵌套类很容易编译它(没有充分的理由不使用嵌套类)。但是,您的代码不会实例化任何模板。当你尝试这样做时,真正的问题就开始了。

以下是您需要应用的差异:

friend class const_iterator;                                      (ca. line 26) ===> 
class const_iterator;
class const_iterator                                              (ca. line 98) ===> 
template <typename NodeType, typename EdgeType>                               
class SparseGraph<NodeType, EdgeType>::const_iterator
const_iterator SparseGraph<NodeType, EdgeType>::begin() const     (ca. line 301) ===>
typename SparseGraph<NodeType, EdgeType>::const_iterator
SparseGraph<NodeType, EdgeType>::begin() const
const_iterator SparseGraph<NodeType, EdgeType>::end() const       (ca. line 308) ===>
typename SparseGraph<NodeType, EdgeType>::const_iterator
SparseGraph<NodeType, EdgeType>::end() const

但是,您的代码不会实例化任何模板。当您尝试创建const_iterator并递增它时,真正的问题就出现了。也就是说,您的边缘列表、顶点列表和邻接列表类型及其迭代器、常量迭代器和const_iterators都混合在一个大沙拉碗中。它们需要仔细分离和清理。

const_iterator类也没有复制/移动 ctor,可能还有其他小问题。