嵌套迭代器类,begin()和end()sentinel问题

时间:2014-01-07 08:09:06

标签: c++ class templates vector iterator

我遇到嵌套迭代器类的问题。 end()sentinel返回0,我很丢失!任何帮助,将不胜感激。我希望这个程序能够采取任何类型的输入,例如一个int,创建向量,并能够填充数组,直到达到end()sentinel,例如键入Q或任何非整数。我拿了几行不必要的代码来缩短它。

template <class T>
class Set
{
    private:
        vector<T> m_element;
        int size;
    public:
        Set() : size(0) {};
        ................
        ................
        ................

        class iterator;
        friend class iterator;
        class iterator
        {
            private:
                int i_index;
                Set& s;
            public:
                iterator(Set& is) :s(is), i_index(0) {}
                iterator(Set& is, bool) : s(is), i_index(s.size) {}
                int operator++()
                {
                    return s.m_element[++i_index];
                }
                (other postfix ++ operator)
                int current() const
                {
                    return s.m_element[i_index];
                }
                ....................
                bool operator!=(const iterator &rv) const
                {
                    return i_index != rv.i_index;
                }
                .............................
                int getI()
                {
                    return i_index;
                }
        };
        iterator begin() { return iterator(*this);}
        iterator end() {return iterator(*this, true);}
};

template <class T>
istream& operator>> (istream &in, Set<T> &element)
{
    T elementin;
    Set<T> is;
    class Set<T>::iterator it = element.begin();
    while(it != element.end())
    {
        in >> elementin;
        element.push(elementin);
    }
    return in;
}

int main()
{
    Set<int> intset;
    Set<int>::iterator it = intset.begin();
    for(int i = 0; i < 5; i++)
    {
        intset.push(i);
        cout << it.getI();
        ++it;
    }

    cout << endl << intset.begin() << intset.end();

    return 0;
}

OUTPUT:
01234
00

2 个答案:

答案 0 :(得分:2)

结束迭代器指向“过去 - 结束”位置。你不应该试图取消引用它。

此外,您可能不希望打印intset.begin()(它是迭代器,即指针),而*(intset.begin())是(指向)元素。

答案 1 :(得分:2)

首先,你应该提供简短,自包含,正确的例子(SSCCE)。

下面的程序如何解决您所面临的问题,假设我已正确地假设某些功能:

#include <iostream>
#include <vector>

using namespace std;


template <class T>
class Set
{
  private:
    vector<T> m_element;
    int size;
  public:
    Set() : size(0) {};
    Set(const Set& rhs)
    {
       m_element = rhs.m_element;
       size = rhs.size;
    }

    void push(T data)
    {

      m_element.push_back(data);
      ++size;
    }

    class iterator;
    friend class iterator;
    class iterator
    {
      private:
        int i_index;
        Set& s;
      public:
        iterator(Set& is) :s(is), i_index(0) {}
        iterator(Set& is, bool) : s(is), i_index(s.size) {}
        iterator(Set& is, bool val,bool last) : s(is), i_index(s.size) {

          cout<<endl<<"size "<<s.size<<endl;
        if(last)
          i_index = s.size -1 ;
        }

        T operator*()
        {
          return s.m_element[i_index];
        }

        int operator++()
        {
          return s.m_element[++i_index];
        }
        int current() const
        {
          return s.m_element[i_index];
        }
        bool operator!=(const iterator &rv) const
        {
          return i_index != rv.i_index;
        }
        int getI()
        {
          return i_index;
        }
    };
    iterator begin() { return iterator(*this);}
//function to check if we have passed last element
        iterator end() {return iterator(*this, true);}
//function to get last element 
        iterator last() {return iterator(*this, true,true);}

};

  template <class T>
istream& operator>> (istream &in, Set<T> &element)
{
  T elementin;
  Set<T> is;
  class Set<T>::iterator it = element.begin();
  while(it != element.end())
  {
    in >> elementin;
    element.push(elementin);
  }
  return in;
}

int main()
{
  Set<int> intset;
  Set<int>::iterator it = intset.begin();
  for(int i = 0; i < 5; i++)
  {
    intset.push(i);
    cout << it.getI();
    ++it;
  }

// cout << endl << intset.begin() << intset.end();
//We use last() to print the last element as end() confirms 
//that we have passed last element and does not point to last element.
  cout << endl << *(intset.begin()) << " " <<*(intset.last())<<endl;

  return 0;
}