problem description:
in learning the knowledge of the CPP template, the program uses last to represent the last position of the saved table item, and during initialization, the last=-1, is initialized to an empty table (my understanding is that there is only one element, and the last position in the table is 0, so-1 means an empty table, so it should be so.) my problem is that there is no new assignment of last when the member function is defined in the book. The program gives me the impression that last has indicated the last position of the table item at this time, but last has obviously been assigned to-1 in the initialization, so can the member function still achieve its function? I don"t know where I misunderstood it, please give me some advice!
here is the program code:
-sharpinclude<iostream>
using namespace std;
template<typename T,int size>class seqlist{
Tslist[size]; //,T slist[size],T "1." bug
int Maxsize; //
int last; //
public:
seqlist(){last=-1;Maxsize=size;} //
//
int Length()const{return last+1;} //
int Find(T&x)const; //x
bool IsIn(T&x); //...
... ...
};
//
template<typename T,int size>int seqlist<T,size>::Find(T & x)const{
int i=0;
while(i<=last && slist[i]!=x)iPP; //x
if(i>last) return -1; //-1
else return i; //
}
take the definition of this member function as an example. Last represents the last position of the table item, but there is no new assignment to last in the program, such as last=size-1;. Last is still the initialization value of-1, isn"t it? If you don"t understand, ask for a big answer.
the following is all the code for this example in the book: