迭代器问题

时间:2011-02-03 02:56:59

标签: c++ arrays iterator

在arrayList中我自定义迭代器,然后使用stl和迭代器排序字符串(单词)。

   **//arrayList.h**
template<class T>
class arrayList {
public:
    // constructor, copy constructor and destructor
    arrayList(int initialCapacity = 10);
    arrayList(const arrayList<T>&);
    ~arrayList() {
        delete[] element;
    }

    // ADT methods
    bool empty() const {
        return listSize == 0;
    }
    int size() const {
        return listSize;
    }
    T& get(int theIndex) const;
    int indexOf(const T& theElement) const;
    void erase(int theIndex);
    void insert(int theIndex, const T& theElement);
    void output(ostream& out) const;

    // additional method
    int capacity() const {
        return arrayLength;
    }
    void reverse();

    // iterators to start and end of list
    class Pointer;
    Pointer begin() {
        return Pointer(element);
    }
    Pointer end() {
        return Pointer(element + listSize);
    }

    // iterator for arrayList
          class iterator
          {
             public:
                // typedefs required by C++ for a bidirectional iterator
                typedef bidirectional_iterator_tag iterator_category;
                typedef T value_type;
                typedef ptrdiff_t difference_type;
                typedef T* pointer;
                typedef T& reference;

                // constructor
                iterator(T* thePosition = 0) {position = thePosition;}

                // dereferencing operators
                T& operator*() const {return *position;}
                T* operator->() const {return position;}

                // increment
                iterator& operator++()   // preincrement
                          {++position; return *this;}
                iterator operator++(int) // postincrement
                          {iterator old = *this;
                           ++position;
                           return old;
                          }

                // decrement
                iterator& operator--()   // predecrement
                          {--position; return *this;}
                iterator operator--(int) // postdecrement
                          {iterator old = *this;
                           --position;
                           return old;
                          }

                // equality testing
                bool operator!=(const typename arrayList<T>::iterator right) const
                      {return position != right.position;}
                bool operator==(const typename arrayList<T>::iterator right) const
                      {return position == right.position;}
             protected:
                T* position;
          };  // end of iterator class


    //      class Pointer: public arrayList<T>::iterator {

    class Pointer:public arrayList<T>::iterator{

    public:
        typedef random_access_iterator_tag iterator_category;
        typedef T value_type;
        typedef ptrdiff_t difference_type;
        typedef T* pointer;
        typedef T& reference;
        // constructor
        Pointer(T *thePosition) {
            position = thePosition;
        }

        Pointer(const Pointer & rhs)
        {
            Pointer(rhs.position);
        }
        //arithmetic operators
        Pointer operator+(int n) const;
        Pointer & operator+=(int n) ;
        Pointer operator-(int n) const ;
        Pointer & operator-=(int n) ;
        reference operator[](difference_type& n)const ;
        bool operator<(const Pointer &rhs) const ;
        bool operator<=(const Pointer & rhs) const;
        bool operator >(const Pointer & rhs) const;
        bool operator >=(const Pointer &rhs) const ;
        int operator -(Pointer rhs) ;

        T operator*() const ;
        T* operator->() const;

        // increment
        Pointer& operator++() ;
        Pointer operator++(int) ;

        // decrement
        Pointer& operator--() ;
        Pointer operator--(int) ;

        // equality testing
        bool operator!=(const Pointer right) const;
        bool operator==(const Pointer right) const;
    protected:
        T* position;
    };




protected:
    T* element; // 1D array to hold list elements
    int arrayLength; // capacity of the 1D array
    int listSize; // number of elements in list
};



**// main.cpp**

int main() {
     .....
    sort(dict.begin(),dict.end(),compare_nocase);/////// error
      ....
    return 0;
}

错误是:

    c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:138:7:   instantiated from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = arrayList<std::basic_string<char> >::Pointer, _ForwardIterator2 = arrayList<std::basic_string<char> >::Pointer]'
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:111:6:   instantiated from 'void std::__move_median_first(_Iterator, _Iterator, _Iterator, _Compare) [with _Iterator = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]'
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:2260:7:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]'
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:2302:62:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = arrayList<std::basic_string<char> >::Pointer, _Size = int, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]'
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5250:4:   instantiated from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = arrayList<std::basic_string<char> >::Pointer, _Compare = bool (*)(std::basic_string<char>, std::basic_string<char>)]'
..\hw3prob2.cpp:53:45:   instantiated from here
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:101:11: error: no matching function for call to 'swap(std::basic_string<char>, std::basic_string<char>)'
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/move.h:106:5: note: candidates are: void std::swap(_Tp&, _Tp&) [with _Tp = std::basic_string<char>]
c:\wascana\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/basic_string.h:2564:5: note:                 void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]

我查了好几个小时但没有想法。谢谢你的帮助。

Edit

最后,它有效。非常感谢!!!!!

2 个答案:

答案 0 :(得分:3)

arrayList<T>::seamlessPointer不满足随机访问迭代器的要求。一目了然,operator*应该返回reference类型,而不是T。它应该有一个默认的构造函数。

编辑:此外,此构造函数不初始化构造的对象。

seamlessPointer(const seamlessPointer & rhs)
{
    seamlessPointer(rhs.position);
}

它构造一个临时的匿名seamlessPointer,然后忘记它。你想要像

这样的东西
seamlessPointer(const seamlessPointer & rhs)
    : position(rhs.position)
{
}

答案 1 :(得分:1)

我认为您的问题是operator*函数的返回类型错误。现在是

T operator*() const

什么时候应该

T& operator*() const

目前,您返回正在迭代的值的副本,因此当std::sort在内部调用std::swap尝试交换值时,它将通过在数组元素的副本而不是实际的数组元素本身(从技术上讲,rvalues而不是lvalues)。将返回类型更改为T&意味着您返回的左值确实可以交换。

尝试更改此内容,看看它是否修复了任何内容。