Listing 1: Cohen's example: insertion operators require const parameters

#include <algorithm>
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
#pragma warning(disable: 4786)
     
class foo
{
public:
  foo(int x_=0, int y_=0) : 
    x(x_),y(y_){}
  bool 
  operator< (const foo& rhs) const
  {return ((x==rhs.x) ? 
     (y < rhs.y) : (x < rhs.x));}
private:
  int x;
  int y;
     
  //this won't compile when used 
  //with an ostream iterator: 
  //friend ostream& operator << 
  //  (ostream& os, foo& f) 
  //{return os << f.x << " " << 
  //  f.y << " ";}
     
  // this is okay:
  friend ostream& operator << 
    (ostream& os, const foo& f);
  {return os << f.x << " " << 
    f.y << " ";}
};
     
typedef set<foo> fooSet;
     
void main()
{
  fooSet foos;
  for (int i=0; i < 10; i++)
    foos.insert(foo(i,i+1));
  ostream_iterator<foo> 
  outFoo(cout);
  // this line won't compile unless 
  // the insertion operator takes a 
  // const foo&
  copy(foos.begin(), foos.end(),  
    outFoo);
}
//End of File