iterator int template class
I am trying to create a Haskell-like list. So I wrapped a normal C++ list. I have a problem though declaring an iterator:
Code:
template <class T>
class hList
{
list<T> data;
public:
hList<T> head() {
hList<T>* tail = new hList<T>;
if (this->data.size() == 0) return *tail;
tail->data.push_back(*(this->data.begin()));
data.pop_front();
return *tail;
}
hList<T> operator+(hList<T>& l) {
data.insert(data.end(), l.data.begin(), l.data.end());
return *this;
}
hList<T> sel(hList<T>& l, bool (*fun)(const hList<T>&, const hList<T>&)){
list<T>::iterator it; // <============= error
}
};
I assume you cannot declare an iterator since the T class isn't known?
In any case what should I do? I wan to iterate through all the elements of list<T> data.
My goal is to do this:
Code:
template <class T>
hList<T> qsortH(hList<T>& l)
{
hList<T> h = l.head();
if (h.data.size() == 0) return l;
return qsort(l.sel(l,lesser) + h + qsort(l.sel(l,greater)));
}
to simulate this example showing how simple and easy read Haskell can be:
Code:
qsort1 [] = []
qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater
Any other suggestions are welcome