用自己的比较函数构造STL的set
2009-12-01 13:36
369 查看
http://www.cppreference.com/wiki/stl/set/start
set<Key, Compare, Alloc>
The C++ Set is an associative STL container that contains a sorted set of unique objects of type Key. Sorting is done using the key comparison function Compare (default is set to std::less<Key>)
#include<iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
class H
{
public:
H(int x=0)
:n(x){}
const int getN() const
{
return n;
}
int setN(int x)
{
int tmp=n;
n=x;
return tmp;
}
ostream & Print(ostream& os) const
{
os << n;
return os;
}
const bool operator<(const H& h)const
{
return n<h.getN();
}
private:
int n;
};
ostream & operator << (ostream &os ,const H &h)
{
return h.Print(os);
}
int main()
{
set<H> s;
int i;
for(i=10;i>0;--i){
s.insert(H(i));
}
copy(s.begin(),s.end(),ostream_iterator<H>(cout," "));
cout << endl;
set<H>::iterator is=s.find(10);
cout << *is << endl;
}
//////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
class H
{
public:
H(int x=0)
:n(x){}
const int getN() const
{
return n;
}
int setN(int x)
{
int tmp=n;
n=x;
return tmp;
}
ostream & Print(ostream& os) const
{
os << n;
return os;
}
private:
int n;
};
ostream & operator << (ostream &os ,const H &h)
{
return h.Print(os);
}
class Hcmp
{
public:
bool operator()(const H& h1,const H& h2) const
{
return h1.getN() < h2.getN();
}
};
int main()
{
set<H,Hcmp > s;
int i;
for(i=10;i>0;--i){
s.insert(H(i));
}
copy(s.begin(),s.end(),ostream_iterator<H>(cout," "));
cout << endl;
set<H>::iterator is=s.find(10);
cout << *is << endl;
}
set<Key, Compare, Alloc>
The C++ Set is an associative STL container that contains a sorted set of unique objects of type Key. Sorting is done using the key comparison function Compare (default is set to std::less<Key>)
#include<iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
class H
{
public:
H(int x=0)
:n(x){}
const int getN() const
{
return n;
}
int setN(int x)
{
int tmp=n;
n=x;
return tmp;
}
ostream & Print(ostream& os) const
{
os << n;
return os;
}
const bool operator<(const H& h)const
{
return n<h.getN();
}
private:
int n;
};
ostream & operator << (ostream &os ,const H &h)
{
return h.Print(os);
}
int main()
{
set<H> s;
int i;
for(i=10;i>0;--i){
s.insert(H(i));
}
copy(s.begin(),s.end(),ostream_iterator<H>(cout," "));
cout << endl;
set<H>::iterator is=s.find(10);
cout << *is << endl;
}
//////////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<set>
#include<iterator>
#include<algorithm>
using namespace std;
class H
{
public:
H(int x=0)
:n(x){}
const int getN() const
{
return n;
}
int setN(int x)
{
int tmp=n;
n=x;
return tmp;
}
ostream & Print(ostream& os) const
{
os << n;
return os;
}
private:
int n;
};
ostream & operator << (ostream &os ,const H &h)
{
return h.Print(os);
}
class Hcmp
{
public:
bool operator()(const H& h1,const H& h2) const
{
return h1.getN() < h2.getN();
}
};
int main()
{
set<H,Hcmp > s;
int i;
for(i=10;i>0;--i){
s.insert(H(i));
}
copy(s.begin(),s.end(),ostream_iterator<H>(cout," "));
cout << endl;
set<H>::iterator is=s.find(10);
cout << *is << endl;
}
相关文章推荐
- STL中sort、priority_queue、map、set的自定义比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- STL中set、map的比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- STL set,map , priority_queue 及 lower_bound 的排序比较函数
- 定义自己的STL map key的类型和比较函数
- C++STL库中string类的find()和findfirstof()函数比较
- STL:set中使用自定义比较操作
- STL常用函数复习之————set
- (转载)虚幻引擎3--UDK常用函数汇总--比较详细解释了一些类中的函数(有用,星月自己备注)
- c++ set<char*>自定义比较函数
- 构造自己的动画函数:animation,stop功能的实现
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- 刚写的 一个数字转中文的小函数,比较常用,自己用~~
- 串口编程 —— 实现 GPS 过程中自己用到的函数 COMMTIMEOUTS,SetCommMask,SetCommTimeouts
- stl排序函数用户自定义比较函数扩展
- 习题 8.17 写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char *p1, char *p2);
- std::set自定义比较函数
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
- 【STL】vector,string,queue,list,set用法及函数总结