Sortowanie zmiennych uporządkowanych i uporządkowanych odwrotnie

0

Witam, mam problem,mianowicie wiem jak wczytywać losowe liczby do tablicy i je sortować, ale nie rozumiem jak mam przerobić to żeby sortowało mi zmienne uporządkowane i uporządkowane odwrotnie. Mógłby ktoś podesłać jakiś kod w C++ i naprowadzić mnie na to ?

//wczytywanie losowych liczb do tablicy
for(int i=0; i<ile; i++)
{
tablica[i] = rand()%100000+0;
}

1

Rosnąco

std::sort(tablica, tablica + ile, [](int a, int b){ return a < b;});

Malejąco

std::sort(tablica, tablica + ile, [](int a, int b){ return a > b;});
0
#include <iostream>
#include <algorithm>
#include <iterator>
#include <random>
#include <functional>

std::mt19937 gen{ std::random_device{}() };
std::uniform_int_distribution<int> dist(1, 100);

template <typename C>
void Print(const C& coll) {
   std::copy(coll.cbegin(), coll.cend(),
      std::ostream_iterator<int>(std::cout, " "));
   std::cout << std::endl;
}

int main() {
   std::vector<int> nums(10);
   for (auto& el : nums) {
      el = dist(gen);
   }
   Print(nums);
   std::sort(nums.begin(), nums.end(), std::less<int>());
   Print(nums);
   std::sort(nums.begin(), nums.end(), std::greater<int>());
   Print(nums);
   std::reverse(nums.begin(), nums.end());
   Print(nums);
   return 0;
}

https://wandbox.org/permlink/kRcsxR4EQLJkQoGd

1 użytkowników online, w tym zalogowanych: 0, gości: 1