STL一般的な用途は、北を指し、
セットとマルチセット
セットおよびマルチセットは、STLの赤黒木(明らかにバイナリ検索ツリー)に基づいています。
定義
我々は使用することができます(multi)set<元素类型>名称
定義します(multi)set
。
カスタム順序
デフォルトのソートは昇順です。何が構造体との間に定義されていないので<
、私たちは、カスタム比較関数を必要としています。
要素が構造ではない場合:
//自定义比较函数myComp,重载“()”操作符
struct myComp
{
bool operator()(const int &a,const int &b)
{
return a > b;//从大到小排序
}
}
set<int,myComp>s;
set<int,myComp>::iterator it;
要素は構造がある場合:
struct Info
{
string name;
float score;
//重载“<”操作符,自定义排序规则
bool operator < (const Info &a) const
{
//按score从大到小排列
return a.score<score;
}
}
set<Info> s;
set<Info>::iterator it;
若しくは
//自定义比较函数myComp,重载“()”操作符
struct myComp
{
bool operator()(const your_type &a,const your_type &b)
{
return a.data > b.data;//按照data从大到小排序
}
}
set<your_type,myComp>s;
set<your_type,myComp>::iterator it;