an example of using STL algorithm sort on a vector<Kid>


👍 g++ -std=c++11 kids_sorted.cpp
👍 ./a.out
Felix 13 Peter 15 Marius 12 
Felix 13 Marius 12 Peter 15 

👍 cat kids_sorted.cpp
#include <iostream>
#include <vector>
using namespace std;

template<typename I>
void prt(I i, I end) {
  for(; i != end; ++i)
    cout << *i << ' ';
  cout << endl;
}

struct Kid {
  string name;
  int age;
  bool operator==(const Kid &k)
    const {
    return name == k.name &&
           age == k.age;
  }
  bool operator<(const Kid &k)
    const {
    return name < k.name ||
      name == k.name && age < k.age;
  }
};

ostream& operator<<(ostream &o, 
                    const Kid &k) {
  o << k.name << ' ' << k.age;
  return o;
}

int main() {
  vector<Kid> v(1, Kid{"Felix", 13});
  v.push_back(Kid{"Peter", 15});
  v.push_back(Kid{"Marius", 12});
  prt(v.begin(), v.end());

  sort(v.begin(), v.end());
  prt(v.begin(), v.end());
}