我創建了一個非常類似於矢量(類項目)的容器類,並定義了一個迭代器子類。其中一個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);
}
};
否;這是一個ctor,所以我不認爲我需要它? – 2012-04-27 00:07:38
我很抱歉的噪音。我確實注意到「Sol」是類的類型。 – Mahesh 2012-04-27 00:08:11
沒有汗,男人;我很欣賞這個問題。 – 2012-04-27 00:13:11