How to stable_sort without copying?
- by Mehrdad
Why does stable_sort need a copy constructor? (swap should suffice, right?)
Or rather, how do I stable_sort a range without copying any elements?
#include <algorithm>
class Person
{
Person(Person const &); // Disable copying
public:
Person() : age(0) { }
int age;
void swap(Person &other) { using std::swap; swap(this->age, other.age); }
friend void swap(Person &a, Person &b) { a.swap(b); }
bool operator <(Person const &other) const { return this->age < other.age; }
};
int main()
{
static size_t const n = 10;
Person people[n];
std::stable_sort(people, people + n);
}