试图重载[]来遍历双重链表

tat*_*o3d 2 c++ linked-list

我一整天都试图弄明白这一点,因为据我所知,我已经在我的重载+和 - 运算符中编写了代码,我需要弄清楚如何重载[]运算符以便在一个值放在它们内部,它将正确遍历列表并指向信息,例如.. [5] 将它向前移动5,[ - 5]将向后移动,任何帮助都会非常感激,就像我说的那样,似乎我几乎已经在我的+和-...中编写了代码.

typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator+(const int amount) const {
    doublyLinkedList<T>::iterator tempClone(*this);
    tempClone.pastBoundary=false;
    T i;

    if(amount < 0)
    {
    return this->operator-(-amount);
    }

    for(i=0; i < amount; i++)
    {   
    if(tempClone.current->forward == NULL)
    {
       tempClone.pastBoundary =true;
    }else
    {
       ++tempClone;
    }
    }
    if(tempClone.pastBoundary == true)
    {
    return *this;
    }else
    {
        return tempClone;   
    }
    }

template <typename T>
typename doublyLinkedList<T>::iterator doublyLinkedList<T>::iterator::operator-(const int amount) const {
    doublyLinkedList<T>::iterator tempClone(*this);
    tempClone.pastBoundary=false;
    T i;

    if(amount < 0)
    {
    return this->operator+(-amount);
    }

    for(i=0; i < amount; i++)
    {   
        if(tempClone.current->backward == NULL)
    {
       tempClone.pastBoundary =true;
    }else
    {
       --tempClone;
    }
    }

    if(tempClone.pastBoundary == true)
    {
    return *this;
    }else
    {
        return tempClone;   
    }
}


template <typename T>
T& doublyLinkedList<T>::iterator::operator[](const int index) {
doublyLinkedList<T>::iterator tempClone(*this);

if(index >= 0){
   return this->operator+(index);
 }else{
   return this->operator-(index);
 }
Run Code Online (Sandbox Code Playgroud)

eca*_*mur 5

operator+返回一个迭代器,所以operator[]应该间接返回值:

template <typename T>
T& doublyLinkedList<T>::iterator::operator[](const int index) {
  return *(this + index);
}
Run Code Online (Sandbox Code Playgroud)

如在其它地方所提到的,这是误导性的,以提供operator+operator[]用于非随机存取容器中,作为O(n)的性能可能会令人吃惊.