iterator begin();
const_iterator begin() const;
const_iterator cbegin() const;
这是有点不清楚,基于文档,但它看起来像begin()将返回第一个头节点(又名根节点)。
http://www.dcs.gla.ac.uk/~samm/trees.html
更新
#include <iostream>
#include <algorithm>
#include <boost/intrusive/rbtree.hpp>
using namespace boost::intrusive;
struct X : public set_base_hook<optimize_size<true> > {
X(int x) : _x{x} { }
int _x;
friend inline std::ostream& operator<<(std::ostream&, const X&);
friend bool operator<(const X&, const X&);
friend bool operator>(const X&, const X&);
friend bool operator==(const X&, const X&);
};
std::ostream& operator<<(std::ostream& os, const X& x) {
os << x._x;
return os;
}
bool operator<(const X& lhs, const X& rhs) { return lhs._x < rhs._x; }
bool operator>(const X& lhs, const X& rhs) { return lhs._x > rhs._x; }
bool operator==(const X& lhs, const X& rhs) { return lhs._x == rhs._x; }
int main()
{
typedef rbtree<X> tree_t;
tree_t tree;
X x0(0);
X x1(1);
X x2(2);
/*! Output is the same for the following
* X x1(1);
* X x0(0);
* X x2(2);
*/
tree.insert_unique(x1);
tree.insert_unique(x0);
tree.insert_unique(x2);
std::for_each(
tree.begin(), tree.end(),
[](const X& xx) { std::cout << "x: " << xx << std::endl; });
}
输出
X:0 X:1 X:2
我注意到push_back/push_front不会调用树重新排序。也许我错过了文档。
它似乎并不像它。调用'begin()'似乎返回第一个元素,由比较函数决定。 –
感谢您的回复,但仍然需要顶层(root/header)节点,而不是最左边/最右边的节点。 –