fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <utility>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. constexpr int N = 4;
  11. vector<vector <int> > P {{1, 0, 0, 0}, {0, 0, 0, 1}, {1, 1, 0, 0}, {1, 0, 0, 0}};
  12. std::vector<double> f = {3, 3, 2, 3};
  13. for(int i=0;i<4;i++)
  14. {
  15. for(int j=0;j<4;j++)
  16. {
  17. cout<<P[i][j];
  18. }
  19. cout<<endl;
  20. }
  21. cout<<endl;
  22. vector< pair<double, vector<int> > > X;
  23. for (int i=0;i<N;i++)
  24. X.push_back(make_pair(f[i],P[i]));
  25.  
  26. ////Sorting fitness descending order
  27. stable_sort(X.rbegin(), X.rend(), [](const auto&lhs, const auto& rhs) { return lhs.first < rhs.first; });
  28. for(int i=0;i<4;i++)
  29. {
  30. P[i]=X[i].second;
  31. f[i]=X[i].first;
  32. }
  33. for(int i=0;i<N;i++)
  34. {
  35. for(int j=0;j<4;j++)
  36. {
  37. cout<<P[i][j];
  38. }
  39. cout<<endl;
  40. }
  41. }
Success #stdin #stdout 0s 3284KB
stdin
Standard input is empty
stdout
1000
0001
1100
1000

1000
0001
1000
1100