簡體   English   中英

C ++:在仿STL類中使用任何迭代器類型?

[英]C++: Using any iterator type in a faux-STL class?

我正在創建一個與矢量(類項目)非常相似的容器類,並定義了一個迭代器子類。 其中一個ctor接受兩個迭代器參數,並使用它們表示的半開范圍構建數據結構(雙鏈表)。

下面的代碼(幾乎)適用於使用兩個Sol :: iterator類型的迭代器調用insert(iterator,iterator)的情況(出於某種原因,* ita始終指向正確的值,而insert(* ita)調用卻沒有)似乎沒有增加價值?)。 (更大的)問題是,插入調用需要適用於所有迭代器類型(例如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);
    }
   };

(更大的)問題是,插入調用需要適用於所有迭代器類型(例如vector :: iterator),而我卻無法追蹤如何使其工作。

您需要此方法才能對所有迭代器類型起作用,這一事實表明它應該是模板函數。 如果查看一下std::list容器提供的insert()函數,您會發現其中之一具有以下聲明:

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

只要該類型提供了insert()函數使用的操作,它就可以使用任何類型的firstlast (指定要插入的半開范圍insert() 您應該考慮做類似的事情。 只要您的insert()函數僅對這些參數執行以下操作,您就應該能夠使用幾乎所有迭代器(輸出迭代器除外):

  • 相比
  • 取消引用
  • 增量

您在iterator類的末尾缺少分號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM