//c++ 中结构体重载操作符的三种方式
 struct node{

int x;

node(int val):x(val){}

bool operator<(const node &a)const{

   return x < a.x ;
}


};

struct node1{
int xx;

node1(int val):xx(val){}


friend bool operator<(const node1  &a, const node1  &b)
{
   return a.xx < b.xx;
}

};

struct node2{
int xy;

node2(int val):xy(val){}

};

bool operator<(const node2 a, const node2 b)
{
  return a.xy < b.xy;
}

int main(int argc, char** argv)
{

   node tm1(1), tm2(2);
   std::cout<<(tm1<tm2)<<std::endl;
   
   node1 ttm1(5), ttm2(2);
   std::cout<<(ttm1<ttm2)<<std::endl;
   
   node2 tttm1(7), tttm2(2);
   std::cout<<(tttm1<tttm2)<<std::endl;
   
   
   
   
   
   
   return 0;
  
}

编译

运行

./test
1
0
0

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐