fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. static const int N = 4;
  8.  
  9. class Comparator {
  10. public:
  11. bool operator()(int lhs, int rhs) {
  12. return lhs > rhs;
  13. }
  14. };
  15.  
  16. typedef priority_queue<int, vector<int>, greater<int>> MinHeap;
  17.  
  18. void PrintSortedMatrix(const int m[][N], size_t n) {
  19. if (n == 0)
  20. return;
  21.  
  22. MinHeap minHeap;
  23.  
  24. for (size_t i = 0; i < n; i++) {
  25. for (size_t j = 0; j < n; j++) {
  26. minHeap.push(m[i][j]);
  27. }
  28. }
  29.  
  30. // print it
  31. cout << "Print matrix in sorted order: " << endl;
  32. while(!minHeap.empty()) {
  33. cout << minHeap.top() << ", ";
  34. minHeap.pop();
  35. }
  36.  
  37. cout << endl;
  38. }
  39.  
  40. int main() {
  41. // your code goes here
  42. int m[][N] {{0,1,2,3},
  43. {1,2,3,4},
  44. {2,3,4,5},
  45. {3,4,5,6}};
  46.  
  47. PrintSortedMatrix(m, N);
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Print matrix in sorted order: 
0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6,