3

I am trying to learn C++ STL..I came across this syntax,not able to figure out what the whole line means!

  struct student{ 
   int id,pts;

bool operator < (student x) const{
    return pts>x.pts;
}
}a[150000];
user2864740
  • 60,010
  • 15
  • 145
  • 220
  • It's an overload for the `<` operator on the `student` type. The function name is `operator<` but it can be called like: `if (some_student < other_student)`. The `bool` is just the return type of the function. Note that it looks like there's a bug: the return expression should probably be `pts < x.pts`. – Michael Burr Aug 14 '15 at 01:05
  • [Operator overloading](http://stackoverflow.com/q/4421706/3309790) – songyuanyao Aug 14 '15 at 01:40

3 Answers3

3

it is defining a "less than" operator for the struct student, so that people can write:

 student one, two;
 bool b = one < two;
Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
1

operator< allows comparing two students, and in this example, it compares only by pts.

struct student{ 
   int id,pts;

   bool operator < (student x) const{
    return pts>x.pts; // must be pts<x.pts
  }
}a[150000];

lest should it work the other way (operator >), the implementation must use operator '<'

As a good practice, please consider adding custom definition for operator > and operator=, because the comparison is based only on pts (by the definition of operator <)

For the sake of minimal completeness and logical correctness when using comparison operators, please consider adding custom definition for operators > and ==

struct student{ 
   int id,pts;

   bool operator < (student x) const{
      return pts<x.pts; 
   }   
   bool operator > (student x) const{
      return pts>x.pts; 
   }
   bool operator == (student x) const{
      return pts == x.pts; 
   }

}a[150000];

Arun
  • 2,087
  • 2
  • 20
  • 33
1

operator < is a name to be defined, just like foo or bar. It behaves just like an identifier.

bool operator < (student x) const declares a function as bool foo (student x) const would.

The effect is to define usage of the < operator between student objects. The connection to the STL is that templates related to ordering of objects use expressions like a < b to perform the ordering. This comes up in algorithms like std::sort and containers like std::map.

That particular definition sorts students by points, which isn't a very good system for sorting students. Facilities like sort and map always provide an alternative to operator <, where you can pass a more specific function, say one called less_points. Also, since that definition of operator < uses the > operator internally (without reversing the left- and right-hand sides), it will sort in descending order whereas the usual convention is to use ascending order.

Note, it's common to define operator < or operator == without defining >, !=, or the other relational operators. The standard library only ever cares about < and ==, and boilerplate to get the rest is usually wasted. Likewise it's a good convention to avoid using relations besides < and == on objects that might not be numbers.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • +1 for bringing out the STL relation, what if I want a custom operator > (descending order)? without a custom operator >, !(myStudent < yourStudent) is not the same as (myStudent > yourStudent)? – Arun Aug 14 '15 at 01:41
  • @Arun The standard library never looks at `operator >`, so defining it gets you no closer to a descending ordering. It's better to define a specific function like `more_points`. You can also define `operator >` and specify `std::greater`, or specify `std::rel_ops::operator > ` as the custom function, which is defined in terms of `operator <`. – Potatoswatter Aug 14 '15 at 02:01
  • Ah I see, thank you @Potatoswatter for the explanation! – Arun Aug 14 '15 at 02:06