alansam

Reverse a list [array] of integers.

Dec 12th, 2021 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4.  
  5. int main() {
  6.   auto show = [](auto const & sv) {
  7.     for (auto const nr : sv) {
  8.       std::cout << std::setw(3) << nr;
  9.     }
  10.     std::cout << std::endl;
  11.   };
  12.  
  13.   int array[] { 10, 2, 0, 14, 43, 25, 18, 1, 5, 45, 3, 15, 16, 8, 9, };
  14.   size_t constexpr array_sz = sizeof(array) / sizeof(*array);
  15.   show(array);
  16.   std::sort(std::begin(array), std::end(array));
  17.   show(array);
  18.   std::cout << std::endl;
  19.   int reverse[array_sz];
  20.   std::copy(std::crbegin(array), std::crend(array), std::begin(reverse));
  21.   show(reverse);
  22. }
  23.  
Add Comment
Please, Sign In to add comment