fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct state {
  7. string name; //name of state
  8. //struct county *c; //name of counties
  9. int counties; //number of counties in state
  10. int population; //total population of state
  11.  
  12. bool operator< (const state& rhs) {
  13. return lexicographical_compare(name.cbegin(), name.cend(), rhs.name.cbegin(), rhs.name.cend());
  14. }
  15.  
  16. bool operator== (const state& rhs) {
  17. return name == rhs.name;
  18. }
  19. };
  20.  
  21. int main() {
  22. state array[] = {{"happy"s, 1, 2}, {"sad"s, 3, 4}, {"normal", 5, 6}};
  23.  
  24. sort(begin(array), end(array));
  25.  
  26. for(auto& i : array){
  27. cout << i.name << endl;
  28. }
  29. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
happy
normal
sad