Inhaltsverzeichnis

sort()

#include <algorithm>

void sort (Ran first, Ran last) 
void sort (Ran first, Ran last, Comp comp) 
void sort (Exec pol, Ran first, Ran last) 
void sort (Exec pol, Ran first, Ran last, Comp comp) 
 
Ran ranges::sort (Range r, Comp comp = {}, Proj proj = {}) 
Ran ranges::sort (Ran first, Ran last, Comp comp = {}, Proj proj = {}) 

Sortiert den Bereich [first,last) in aufsteigender Folge.

Parameter

first Anfang des Bereiches
last Ende des Bereiches
comp Vergleichskriterium (Vorgabe = less)
proj einstelliger Funktor (Vorgabe = std::identity)
pol parallele Ausführungsart

Ergebnis

Rückgabewert: keiner bzw. last

Siehe auch

is_sorted(), partition(), partial_sort(), stable_sort().

Beispiel

sort.cpp
#include <algorithm>
#include <iostream>
#include <string>
 
int main()
{
  std::string s = "ein Beispiel";
  std::cout << s << '\n';
 
  std::sort(begin(s), end(s));
 
  std::cout << s << '\n';
}
rg_sort.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
 
struct Person
{
  std::string name;
  int born;
  int died;
};
 
auto lifespan = [](Person p) { return p.died - p.born; };
 
void show(const std::vector<Person>& persons, std::string message)
{
  std::cout << '\n' << message << "\n\n";
 
  for (auto p : persons)
  {
    std::cout 
      << p.name << '\t' 
      << p.born << '-' 
      << p.died << ' ' 
      << lifespan(p) << '\n';
  }
}
 
int main()
{
  auto v = std::vector<Person> 
  {
    { "Galilei", 1564, 1642 },
    { "Newton" , 1642, 1726 },
    { "Hawking", 1942, 2018 },
  };
 
  namespace rg = std::ranges;
 
  rg::sort(v, {}, &Person::name);  
  show(v, "--- sorted by name:");  
 
  rg::sort(v, {}, &Person::born);  
  show(v, "--- sorted by year of birth:");
 
  rg::sort(v, rg::greater{}, lifespan);  
  show(v, "--- sorted by lifespan:");  
}