std::sort doesn't work (c++11)

I Implementer class Which has its own iterator, I would apply the sort function of std but it does not work, I was told that the operator - is absent. Yet count feature to function, and also accumulate: Here are my souce code. thank you in advance.

===================== ======================================
#ifndef LINKED_LIST2_H
#define LINKED_LIST2_H
#include <iostream>

template<class T>

class Linked_list2{

private:

class ListElement{

public:
ListElement *prev, *next;
T data;

ListElement(const ListElement & e){
prev = next = nullptr;
data = e.data;
}

ListElement(T & e){
prev = next = nullptr;
data = e;
}

};

ListElement *first,*last;
int size;

public:

Linked_list2()
{
first = last = nullptr;
size = 0;
}

void copy(Linked_list2<T> & l){
Linked_list2(); // Appel de la fonction copie a voir
ListElement *current = l.first;

while(current != nullptr){

ListElement *element = new ListElement(current->data);
if(first==nullptr){
first = last = element;
}else{
last->next = element;
element->prev = last;
last = element;
}
size++;
current = current->next;
}
}

Linked_list2(Linked_list2<T> const & l)
{
copy(const_cast<Linked_list2<T>&>(l));
}

void detruire(){
ListElement *current = first,*next;

while(current != nullptr){
next = current->next;
delete current;
current = next;
}
}

~Linked_list2(){
detruire();
}

friend std::ostream & operator<<(std::ostream & out, Linked_list2<T> const & l){
ListElement *current = l.first;
while(current!=nullptr){
out << current->data << " ";
current = current->next;
}

return out;
}

Linked_list2<T> & operator=(const Linked_list2<T> & l){

if(&l!=this){
detruire();
copy(const_cast<Linked_list2<T>&>(l));
}
}





void put_first(T m){

ListElement *element = new ListElement(m);

if(first==nullptr){
first=last=element;
}else{
element->next = first;
first->prev = element;
first = element;
}
size++;
}

void put_last(T m){

ListElement *element = new ListElement(m);

if(first==nullptr){
first=last=element;
}else{
last->next = element;
element->prev = last;
last = element;
}
size++;

}


// Iterateur

class iterator : public std::iterator<std::random_access_iterator_tag, long int, long int>{
public:
typedef iterator self_type;
typedef T value_type;
typedef T& reference;
typedef ListElement* pointer;
typedef std::forward_iterator_tag iterator_category;
typedef int difference_type;
private:
pointer ptr;
public:
iterator():ptr(nullptr){};
iterator(pointer ptr_) : ptr(ptr_) { }
self_type operator++() { self_type i = *this; ptr = ptr->next; return i; }
self_type operator++(int junk) { ptr = ptr->next; return *this; }
reference operator*() { return ptr->data; }
pointer operator->() { return ptr; }
bool operator==(const self_type& rhs) { return ptr == rhs.ptr; }
bool operator!=(const self_type& rhs) { return ptr != rhs.ptr; }
bool operator<(const self_type& rhs) { return ptr < rhs.ptr; }
bool operator<=(const self_type& rhs) { return ptr <= rhs.ptr; }

};

iterator begin(){
return iterator(first);
}

iterator end(){
return iterator(nullptr);
}

};


#endif
Last edited on
std::sort requires a random access iterator.

To satisfy the requirements for a random access iterators you need to provide operators (-, +, [], ...) to allow jumping from one element to any other element in constant time.

http://en.cppreference.com/w/cpp/concept/RandomAccessIterator

Random access is generally not possible with linked lists. That's why std::list provides it's own sort function.

http://en.cppreference.com/w/cpp/container/list/sort
Last edited on
Thanks for help, but I can not find me, he put me the following error: error: no matching function for call to '__lg (Linked_list2 <int> :: iterator :: self_type)'
Topic archived. No new replies allowed.