开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
FreeBSD 10x
Clang++ 3.5
参数 -Wall (试过 -g, -O1 -O2, 基本上问题都存在或甚至更离谱)
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
使用 stl_tree.h里面的 red black tree
问题(Question):
最近做了一些实验 主要是想看看能不能使用stl_tree.h里面的那个rbtree.
不过遇到了一个很奇怪的问题
主要是当我使用std::less<int> 当作key的比较functor时, 最后结果用inorder
traversal 或是用rbtree的begin() and end()去循序印出的结果是错误的,
用gdb进去看发现传到 std::less<int>::operator()(const int&, const int&)
的值并非正确的值(-1077945256).......
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
节录程式码: 我是直接继承 _Rb_Tree来使用它内部的function, 方便存取内部资料
MyClass
{
public:
struct Key
{
int operator()(const MyClass& class) { return class.getHead();}
};
public:
int getHead() { return head;}
private:
.......
int head;
.......
};
template<typename K,typename V, typename KofV, typenam Comp,
typename Alloc=std::allocator<V> >
MyTree:public std::_Rb_tree<K,V,KofV,Comp,Alloc>
{
public:
std::pair<std::_Rb_tree<K,V,KofV,Comp,Alloc>::iterator,bool >
insert_unique(const value_type& value){this->_M_insert_unique(value);};
}
typedef MyTree< int, MyClass, MyClass::Key, std::less<int> > Tree;
使用插入:
Tree tree;
tree.insert(MyClass(....));
= = = = = = = = = = = = = = = = = = = = = = = = = = = = =
之后我使用inorder traversal 跟 tree.begin() to tree.end() iteration,印出的
顺序是错误的. 可是!!!!!!!! 如果我把比较函数换成另外一个:
struct MyComp
{
bool operator()(int a, int b) { return a < b; }
}
顺序就对了, 注意, 红色标示处一定要是 int, 若是换成const int& 一样出错!!!
另外 std::less<int> 跟 MyComp单独使用不管参数型态为何都是正常的......
请问有谁遇过这种状况吗? 见鬼的奇怪....谁能帮忙试一下g++版本
最近FreeBSD gcc port都编译失败.....坏一阵子了......