0
点赞
收藏
分享

微信扫一扫

C++ rb_tree的几种构造方法

墨春 2022-02-02 阅读 18

在学习rbtree时,看到第四个模板参数为class _Compare,以下为构造rbtree的几种方法

#include <iostream>
#include <bits/stl_tree.h>
#include <functional>
using namespace std;
template <class T>
struct identity : public unary_function<T, T>
{
    const T &operator()(const T &x) const
    {
        return x;
    }
};
struct Mycompare{
	bool operator()(const int a,const int b){return a>b;}
};
int main()
{
    //使用less<>等标准库模板类
    _Rb_tree<int, int, identity<int>, less<int>> tree1;
	auto myCompare= [](int a, int b)-> bool{ return a > b; };
	//报错,因为模板参数需要的是类,而不是函数
	//_Rb_tree<int, int, identity<int>,Mycompare> tree2;
	//自建类重载operator()函数
    _Rb_tree<int, int, identity<int>,Mycompare> tree2;
	//使用std::function<>
	_Rb_tree<int, int, identity<int>,std::function<bool(const int a, const int b)>> rtree;
   	 return 0;
}
举报

相关推荐

0 条评论