CPP sequence table class?

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:



CPP
Mar.10,2021

Note that in insert () , last has a PP action, so last will update itself when you insert it with this sequential table.

and when initializing, last=-1, means initializing 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)

last =-1 just means there are no elements in it. This is done in the constructor.

as for the confusion you mentioned in find () , it is because you must first insert () , and then find () , then last is no longer -1 , because the legal insert () has passed.

PS: but the code wind of this book is very bad, so I think maybe you should find better materials to learn (but it is reasonable that the basic code wind of data structure / algorithm books are a mess. At least this is the case for foreign books (except for clrs, which uses pseudocodes like this), but it is not clear at home.

Menu