fork(2) download
  1. #include <iostream>
  2. #include <array>
  3. #include <algorithm>
  4. #include <cstddef>
  5.  
  6. template <typename T, std::size_t R, std::size_t C>
  7. using Matrix = std::array<std::array<T, C>, R>;
  8.  
  9. int main() {
  10. Matrix<int, 3, 3> matrix = {{ { 2, 3, 4 },
  11. { 7, 8, 9 },
  12. { 5, 6, 7 } }};
  13.  
  14. std::sort(std::begin(matrix), std::end(matrix), [] (decltype(*matrix.cbegin())& lhs, decltype(*matrix.cbegin())& rhs) {
  15. return std::accumulate(std::begin(lhs), std::end(lhs), 0) > std::accumulate(std::begin(rhs), std::end(rhs), 0);
  16. });
  17.  
  18. for (decltype(matrix.size()) row = 0; row != matrix.size(); ++row) {
  19. std::cout << row + 1 << ". ";
  20. for (const auto i : matrix[row]) {
  21. std::cout << i << " ";
  22. }
  23. std::cout << std::accumulate(std::begin(matrix[row]), std::end(matrix[row]), 0) << std::endl;
  24. }
  25. }
  26.  
Success #stdin #stdout 0s 3300KB
stdin
Standard input is empty
stdout
1. 7 8 9 24
2. 5 6 7 18
3. 2 3 4 9