Advertisement
alansam

Reverse a list [std::vector] of integers.

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