C ++:在faux-STL类中使用任何迭代器类型?

时间:2012-04-26 23:56:15

标签: c++ stl iterator

我正在创建一个非常类似于vector(类项目)的容器类,并定义了一个迭代器子类。其中一个ctors接受两个迭代器参数,并使用它们所代表的半开放范围构建数据结构(双链表)。

下面的代码(几乎)适用于使用Sol :: iterator类型的两个迭代器调用insert(迭代器,迭代器)的情况(出于某种原因,* ita始终指向正确的值,但插入(* ita) )调用似乎没有添加值??)。 (更大的)问题是insert调用需要适用于所有迭代器类型(例如vector :: iterator),而我无法找到如何使其工作。我尝试过typedef / typename T :: iterator迭代器,但最安静的g ++是'typename T :: iterator iterator',它返回

g++ ex1.cc -o sol -Wall -Wextra -ansi -pedantic
In file included from ex1.cc:1:0:
Sol.h:87:16: error: expected ‘)’ before ‘ita’
ex1.cc:109:5: error: expected ‘}’ at end of input
In file included from ex1.cc:1:0:
Sol.h:84:3: error: expected unqualified-id at end of input
make: *** [ex1] Error 1

没有多大意义;至少对我而言。这是广泛的招数:

template <class T, int find_val = 1, class Compare = std::less<T> >
class Sol{

public:
    typedef unsigned int size_type; //typedef'ing class variable types
    typedef T key_type; 
    typedef T value_type; 
    //typename T::iterator iter;

private:

    struct node{ //Used as 'links' in the chain that is the doubly linked list
        T data;
        node *prev;
        node *next;
    };

    node *head, *tail; //The head and tail of the dll
    int find_promote_value; //How much should find() shift a value?

public:
    class iterator{
        private:
            node *no;
            friend class Sol;
            iterator(node *p) : no(p){
            }
        public:
            iterator() : no(0){
            }

            iterator operator++(){ //Preincrement
                no = no->next;
                return *this;
            }

            iterator operator++(int){ //Postincrement
                iterator temp = *this;
                ++*this;
                return temp;
            }

            iterator operator--(){  //Predecrement
                no = no->prev;
                return *this;
            }

            iterator operator--(int){  //Postdecriment
                iterator temp = *this;
                --*this;
                return temp;
            }

            T operator*(){
                return no->data;
            }

            T *operator->(){
                return &no->data;
            }

            bool operator==(const iterator &rhs){
                return no == rhs.no;
            }

            bool operator!=(const iterator &rhs){
                return no != rhs.no;
            }       
    };

    Sol() : head(0), tail(0), find_promote_value(find_val){
    }

    Sol(const Sol &rhs) : head(rhs.head), tail(rhs.tail), find_promote_value(rhs.find_promote_value){
    }

    typename T::iterator iterator;
    Sol(iterator ita, iterator itb){ //Sol.h::87. Problem line
        while (ita != itb){
            iterator itr = insert(*ita);
std::cout << "Inserting " << *ita << ",printout: " << *itr <<"\n";
//dump();
            ++ita;
        }
    }

    ~Sol(){
        clear();
    }

    iterator insert(T input){
        node *n = new node;
        n->next = NULL;
        n->prev = tail;
        n->data = input;

        if(!tail){
            head = n;
            tail = n;
        }
        else{
            tail->next = n;
            tail = n;
        }

        return iterator(tail);
    }
   };

2 个答案:

答案 0 :(得分:2)

  

(更大的)问题是insert调用需要适用于所有迭代器类型(例如vector :: iterator),而且我无法找到如何使其工作。

你需要这个适用于所有迭代器类型的事实表明它应该是一个模板化的函数。如果查看insert()容器提供的std::list函数,您会看到其中一个函数具有以下声明:

template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);

允许它使用firstlast的任何类型(指定要插入的半开放范围),只要该类型提供insert()函数使用的操作。你应该考虑做类似的事情。只要你的insert()函数只对这些参数执行以下操作,你应该能够使用几乎任何迭代器(输出迭代器除外):

  • 比较
  • 解引用
  • 增量

答案 1 :(得分:1)

你在iterator课程的末尾错过了一个分号。