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 不会调用树重新排序。也许我在文档中错过了这一点。