C++ 如何使用boost::iterator_facade访问遗留链表

C++ 如何使用boost::iterator_facade访问遗留链表,c++,boost,C++,Boost,作为理解如何使用boost::iterator_facade的一部分,我从这个问题中提取了一些代码并让它运行起来。我完成的代码如下。我的问题涉及std::for_each和std::find算法中的end参数。我保留了我在原始代码中从原始问题中找到的习惯用法。基本上,它使用一种空迭代器作为结束参数。只要我按照所示的方式定义equal()成员,它就可以工作 我的问题是,这个习语(一个空迭代器(?),可能有一个我不知道的真正的术语,是不是这种类型的迭代器),是众所周知的好习惯。如果没有,你能推荐一种

作为理解如何使用boost::iterator_facade的一部分,我从这个问题中提取了一些代码并让它运行起来。我完成的代码如下。我的问题涉及std::for_each和std::find算法中的end参数。我保留了我在原始代码中从原始问题中找到的习惯用法。基本上,它使用一种空迭代器作为结束参数。只要我按照所示的方式定义equal()成员,它就可以工作

我的问题是,这个习语(一个空迭代器(?),可能有一个我不知道的真正的术语,是不是这种类型的迭代器),是众所周知的好习惯。如果没有,你能推荐一种替代方法吗

// Element of linked list (legacy code, can't be changed)
struct SomeLinkedList
{
    const char* bar;
    int lots_of_interesting_stuff_in_here;
    long foo;
    SomeLinkedList* pNext;

    // actually I cheated and added this to get std::find working
    bool operator ==( SomeLinkedList const& other ) const { return foo == other.foo; }
};

class SomeIterator
    : public boost::iterator_facade< SomeIterator, 
                                     const SomeLinkedList, 
                                     boost::forward_traversal_tag >
{
public:
    SomeIterator() : node_( NULL ) {};  // used to create end iterators in examples
    explicit SomeIterator( const SomeLinkedList* p ) : node_( p ) {};

private:
    friend class boost::iterator_core_access;
    void increment() { node_ = node_->pNext; };
    bool equal( SomeIterator const& other ) const { return node_ == other.node_; }
    SomeLinkedList const& dereference() const { return *node_; };
    SomeLinkedList const* node_;
}; // class SomeIterator

void DoSomething( const SomeLinkedList& node )
{
    std::cout << "DoSomething " << node.foo << "\n";
}

void main()
{
    // Ugly but effective way to create a fake linked list for experiments
    SomeLinkedList temp[3];
    memset(temp,0,sizeof(temp));
    temp[0].pNext = &temp[1];
    temp[1].pNext = &temp[2];
    temp[2].pNext = 0;
    temp[0].foo   = 0;
    temp[1].foo   = 1;
    temp[2].foo   = 2;
    temp[2].bar   = "number 2";
    SomeLinkedList* my_list = &temp[0];

    // DoSomething() for each element in list
    std::for_each( SomeIterator(my_list), /*end*/ SomeIterator(), DoSomething );

    // Find one element in the list
    SomeLinkedList obj;
    obj.foo = 2;
    SomeIterator it = std::find( SomeIterator(my_list),  /*end*/ SomeIterator(), obj );
    std::cout << "found " << it->bar << "\n";
    return 0;
}
//链表元素(旧代码,无法更改)
结构SomeLinkedList
{
常量字符*条;
这里有很多有趣的东西;
龙富,;
SomeLinkedList*pNext;
//事实上,我作弊并添加了这个来获得std::find working
bool运算符==(SomeLinkedList常量和其他)常量{return foo==other.foo;}
};
类迭代器
:public boost::迭代器,
const SomeLinkedList,
boost::前向遍历标记>
{
公众:
SomeIterator():node_u3;(NULL){};//用于在示例中创建结束迭代器
显式SomeIterator(const SomeLinkedList*p):节点{(p)};
私人:
朋友类boost::迭代器\u核心\u访问;
void increment(){node\=node\->pNext;};
bool equal(SomeIterator const&other)const{return node_==other.node_;}
SomeLinkedList常量&取消引用()常量{return*node_;};
SomeLinkedList常量*节点;
}; // 类迭代器
void DoSomething(const SomeLinkedList和node)
{

std::cout这是一种常用方法,无需更改。如果查看
迭代器
标题,您将发现
std::istream\u迭代器
,例如,采用类似方法的
end
迭代器是一个特殊的迭代器值(
istream\u iterator()
)这将
true
与完成迭代的任何迭代器进行比较,而不管它是从哪个输入流初始化的。

我还没有检查您的代码,但是对于
end
迭代器,空迭代器似乎是一个非常好的选择,因为它不指向有效的元素。这非常常见,被称为“sentin”al迭代器。您可能需要查看
SomeLinkedList
中的
运算符==
。它不比较两个列表,只比较列表的头元素。从注释中可以看出,您添加它是为了获得
std::find(it1,it2,value)
工作,如果是这样,您可能需要使用
std::find_if(it1,it2,functor)
其中
functor
是一个函数对象,它存储您正在搜索的值,并将比较
操作符()中接收到的节点中的值
使用该期望值…我将mods与原始代码保持在最低限度,因此我没有更改类型名称,但实际上,与其说它是一个链接列表,不如说它是一个可以链接到链接列表中的元素。因此,比较元素正是我想要的,我不想比较整个列表。谢谢您的评论和回复嗯,我必须调查函子,他们现在对我来说有点神秘