我整天都在挠头试图弄清楚这一点,因为据我所知,我已经在重载的 + 和 - 运算符中编写了代码,我需要弄清楚如何重载 [] 运算符,以便当一个值被放置在其中,它将正确遍历列表并指向信息,例如..[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);
}