Sep 29, 2016 at 7:36pm UTC
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 Sep 29, 2016 at 7:37pm UTC
Sep 30, 2016 at 9:16am UTC
Last edited on Sep 30, 2016 at 9:18am UTC
Sep 30, 2016 at 10:06pm UTC
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)'