We need to design std::list with added features

77 views Asked by At

I am aware that std containers are not meant to be inherited because of absence of virtual destructors. So, I have used std::list as a private member. I need help on the iterator of this class.

#pragma once
#ifndef __pk_DList_h__
#define __pk_DList_h__

#include <list>
#include <iterator>

template<class T>
class DList {
public:
DList() { }

DList(int size, T val) { list_(size, val); }

int size() { return list_.size(); }
//typedef list<T>::iterator Iterator;
class   iterator {
public:
iterator(T *p = nullptr) : p_(p) {}
    //operator T*           () { return p_; }
T&      operator*               () { return *p_; }
    //list<T>::iterator operator->  () { return  p_; }
void    operator++              () { if (p_) p_ = p_->next(); }
bool    operator==              (const iterator & rhs) { return p_ == rhs.p_; }
bool    operator!=              (const iterator & rhs) { return p_ != rhs.p_; }
private:
T*  p_;
};


std::list<T>::iterator begin() { return list_.begin(); }
std::list<T>::iterator end() { return list_.end(); }

void pushFront(T elem) {
    list_.push_front(elem);
}
void pushBack(T elem) {
    list_.push_back(elem);
}

void popBack() {
    list_.pop_back();
}

void popFront() {
    list_.pop_front();
}

T back() {
    list_.back();
}

T front() {
    list_.front();
}

bool empty() {
    return (list_.size() > 0)?false:true;
}
private:
std::list<T> list_;

DList(const DList &s) {}

void operator=(const DList &s) {}
};

#endif //__pk_DList_h__

I want to use this class like this..Any help with the iterator would be appreciated. I am struck with only iterator part. I need an example of how to implement this inner class iterator in DList class.

DList<int> myList;
myList.pushFront(12);
myList.pushBack(13);
myList.pushBack(15);
for (std::list<int>::iterator it = myList.begin(); it != myList.end(); it++) 

{
    cout << *it << endl;
}
0

There are 0 answers