64,654
社区成员
发帖
与我相关
我的任务
分享
struct node{
node(int _u, int _w): u(_u), w(_w) { }
bool operator< (const node& rhs) const { return w > rhs.w; }
int u, w;
};
set<node> q;
// 往 q 里添加元素 其中 g[0][i] 的值都是相同的
FOR(i, 1, n-1) {
cost[i] = g[0][i];
q.insert(node(i, cost[i]));
}
struct node{
node(int _u, int _w): u(_u), w(_w) { }
bool operator< (const node& rhs) const
{
return w > rhs.w;
}
bool operator> (const node& rhs) const // greater调用就是这个。。。
{
return w > rhs.w;
}
int u, w;
};
int main()
{
set<node, greater<node> > q;
q.insert( node(0, 1) );
q.insert( node(0, 2) );
q.insert( node(0, 3) );
system("PAUSE");
return 0;
}
template<class _Ty>
struct less
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return (_Left < _Right); // 这里调用node的operator<
}
};
bool operator< (const node& rhs) const
{
if (w==rhs.w)
return u > rhs.u;
return w > rhs.w;
}
bool operator< (const node& rhs) const
{
if (u==rhs.u)
return w > rhs.w;
return u > rhs.u;
}
#include<iostream>
#include<set>
using namespace std;
class node
{
public:
int i;
int j;
node(int i1,int j1):i(i1),j(j1){}
};
class sortCriterion
{
public:
bool operator()(const node &p1,const node &p2) //这里定义自己的排序方法
{
return p1.j<p2.j;
}
};
void main()
{
node node1(1,0),node2(2,0);
set<node,sortCriterion> set1;
set1.insert(node1);
if(set1.insert(node2).second)
{
cout<<"node2 insert success!"<<endl;
}
else
{
}
}