2012-04-26 63 views
0

我創建了一個非常類似於矢量(類項目)的容器類,並定義了一個迭代器子類。其中一個ctors需要兩個迭代器參數,並使用它們表示的半開範圍構建數據結構(雙向鏈表)。 (幾乎)適用於使用兩個類型爲Sol :: iterator的迭代器調用insert(iterator,iterator)的情況(出於某種原因,* ita總是指向正確的值,但insert(* ita)調用似乎沒有增加值??)。 (更大的)問題是插入調用需要適用於所有迭代器類型(例如vector :: iterator),並且我還沒有能夠追蹤如何完成這項工作。我試過的typedef/typename的T ::迭代器迭代器,但最安靜的G ++已經是與「typename的T ::迭代器迭代器」,它會返回C++:在一個faux-STL類中使用任何迭代器類型?

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); 
    } 
    }; 
+0

否;這是一個ctor,所以我不認爲我需要它? – 2012-04-27 00:07:38

+0

我很抱歉的噪音。我確實注意到「Sol」是類的類型。 – Mahesh 2012-04-27 00:08:11

+0

沒有汗,男人;我很欣賞這個問題。 – 2012-04-27 00:13:11

回答

2

(較大的)問題是插入調用需要適用於所有迭代器類型(例如vector :: iterator),並且我還沒有能夠追蹤如何完成該工作。

事實上,你需要這個適用於所有的迭代器類型,這表明它應該是一個模板化函數。如果你看一下由std::list容器提供的insert()功能,你會看到其中一人有以下聲明:

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

這使得它能夠使用任何類型firstlast(其指定半隻要該類型提供insert()函數使用的操作。你應該考慮做類似的事情。只要你insert()功能僅做以下的事情這些參數,你應該能夠使用幾乎任何迭代器(除了輸出迭代器):

  • 比較
  • 提領
  • 增量
+0

您,先生,贏得我所有的互聯網。非常感謝你! – 2012-04-27 00:27:30

1

你錯過了在iterator課結束分號。