fork(1) download
  1. #include <set>
  2. #include <iterator>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. using namespace std;
  7. class Person{
  8. public:
  9. Person(){}
  10. Person(string v , string v1):nazw(v),imie(v1){}
  11. bool operator<(const Person & K) const
  12. {
  13. return ((this->getN()>K.getN())?0:1);
  14. //return ((this->getN()<K.getN())?1:0);
  15. }
  16. string getN()const
  17. {
  18. return nazw;
  19. }
  20. /*
  21. bool operator()(Person & K, Person & K1)
  22. {
  23.   return ((K->getN()<K1.getN())?1:0);
  24. }
  25. */
  26. friend ostream& operator<<(ostream & o , const Person&K)
  27. {
  28. o << K.nazw << " " << K.imie;
  29. return o;
  30. }
  31. friend struct cmp;
  32. private:
  33. string nazw,imie;
  34. };
  35. struct cmp
  36. {
  37. bool operator()(const Person &K , const Person &K1)
  38. {
  39. if (K.getN() < K1.getN())
  40. return true;
  41. if(K1.getN() < K.getN())
  42. return false;
  43. if(K.imie < K1.imie) // will need a friend declaration or a getter() func
  44. return true;
  45. return false;
  46. }
  47. };
  48. int main()
  49. {
  50. //typedef set<Person> kontener_typ;
  51. typedef set<Person,cmp> kontener_typ;
  52. kontener_typ c;
  53. c.insert(Person("Nowak","Jan"));
  54. c.insert(Person("Nowak","Adam"));
  55. c.insert(Person("Kowalski","Jan"));
  56. c.insert(Person("Nowak","Adam"));
  57. c.insert(Person("Iksinski","Adam"));
  58. std::copy (c.begin(), c.end(), ostream_iterator<Person>(cout, " ,"));
  59. std::cout << std::endl;
  60. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Iksinski Adam ,Kowalski Jan ,Nowak Adam ,Nowak Jan ,