手机网站优化,wordpress微信查看密码,郴州seo,wordpress 英文月份如果set的类型是个结构体 我们需要定义重载函数
***set 容器模版需要3个泛型参数#xff0c;如下#xff1a; template class Key,class Compare less key,class Alloc allocclass set {...};
第一个是元素类型#xff0c;必选#xff1b; 第二个指定…如果set的类型是个结构体 我们需要定义重载函数
***set 容器模版需要3个泛型参数如下 template class Key,class Compare less key,class Alloc allocclass set {...};
第一个是元素类型必选 第二个指定元素比较方式缺省为 Less, 即使用 符号比较 第三个指定空间分配对象一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话你的自定义元素类型需要重载 运算操作 (2) 如果你第2个泛型参数不使用默认值的话则比较对象必须具有 () 操作即 bool operator()(const T a, const T b)*
struct node{int l,r;bool operator(const node R)const{return lR.l||(lR.lrR.r);}node(int a,int b):l(a),r(b){}
};
如果结构体本身就有重载函数 用的时候只需写
setnodes;
即可 如果本身比较类型没有运算符重载函数 我们需要重载()
struct node{int l,r;node(int a,int b):l(a),r(b){}
};
struct cmp
{bool operator()(const node L,const node R)const{return L.lR.l||(L.lR.lL.rR.r);}
};
那么其对应的声明方法是
setnode,cmps;
set中还可以使用lower_bound()和upper_bound() 那么如何对结构体二分呢?
如果我们已经定义了一个比较函数那么lower_bound()或是upper_bound()会根据我们写入的重载比较函数 或是比较结构体去比较寻找 例如
#include iostream
#includebits/stdc.h
using namespace std;
struct node{int l,r;node(int a,int b):l(a),r(b){}
};struct cmp
{bool operator()(const node L,const node R)const{return L.lR.l||(L.lR.lL.rR.r);}
};int main()
{setnode,cmps;s.insert(node(1,2));s.insert(node(1,3));s.insert(node(1,4));s.insert(node(5,2));s.insert(node(3,5));setnode::iterator it s.lower_bound(node(1,5));// 输出 3 5coutit-l it-rendl;setnode::iterator it s.lower_bound(node(1,2));// 输出 1 2coutit-l it-rendl;setnode::iterator it s.lower_bound(node(1,0));// 输出 1 2coutit-l it-rendl;setnode::iterator it s.upper_bound(node(1,2));// 输出 1 3coutit-l it-rendl;return 0;
}所以完全是可以那结构体二分的 需要注意的就是比较函数中涉及到几个成员变量那么关于二分的参数结构体就得指定几个参数。