How to stable_sort without copying?

Posted by Mehrdad on Stack Overflow See other posts from Stack Overflow or by Mehrdad
Published on 2012-12-09T20:26:31Z Indexed on 2012/12/10 5:04 UTC
Read the original article Hit count: 124

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);
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about copy-constructor