D语言比较运算符重载

import std.random; 
import std.stdio; 
import std.string; 

struct Box { 
   int volume;  
   int opCmp(const ref Box Box) const { 
      return (volume == Box.volume ? Box.volume - volume: volume - Box.volume); 
   }

   string toString() const { 
      return format(Volume:%s\n, volume); 
   } 
} 

void main() { 
   Box[] Boxes; 
   int j = 10; 

   foreach (i; 0 .. 10) { 
      Boxes ~= Box(j*j*j); 
      j = j-1; 
   } 

   writeln(Unsorted Array); 
   writeln(Boxes);  
   Boxes.sort; 
   writeln(Sorted Array); 
   writeln(Boxes); 
   writeln(Boxes[0]<Boxes[1]); 
   writeln(Boxes[0]>Boxes[1]); 
   writeln(Boxes[0]<=Boxes[1]); 
   writeln(Boxes[0]>=Boxes[1]); 
}

相关文章

// ------------------- import std.stdio; void main(str...
import std.stdio; int main () { /* local variable defi...
import std.stdio; int main(string[] args) { int a = 20...
import std.stdio; int main(string[] args) { int a = 4;...
import std.stdio; int main(string[] args) { int a = 21;...
import std.stdio; int main(string[] args) { uint a = 60...