Error_code List:: retrieve (int position List entry &x)const; Post: If 0< position<n, where n is the number of entries in the list, the function succeeds: the entry at position is copied to x; all List entries remain unchanged Else: The function fails with a diagnostic error code
Error_code List :: retrieve(int position, List_entry &x) const; Post: If 0 ≤ position<n, where n is the number of entries in the List, the function succeeds: The entry at position is copied to x; all List entries remain unchanged. Else: The function fails with a diagnostic error code
Error_code List: replace int position const List entry &x) Post: If 0 s position <n where n is the number of entries in the list, the function succeeds The entry at position is replaced by x; all other entries remain unchanged Else: The function fails with a diagnostic error code void List: traverse (void (*visit)(List entry &) Post: The action specified by function visit has been performed on every entry of the List, beginning at position 0 and doing each in turn
Error_code List :: replace(int position, const List_entry &x); Post: If 0 ≤ position<n, where n is the number of entries in the List, the function succeeds: The entry at position is replaced by x; all other entries remain unchanged. Else: The function fails with a diagnostic error code. void List::traverse(void (*visit)(List entry &)); Post: The action specified by function *visit has been performed on every entry of the List, beginning at position 0 and doing each in turn
6.2 Implementations of Lists Class Templates CA C++ template construction allows us to write code usually code to implement a class that uses objects of an arbitrary, generic type In template code we utilize a parameter enclosed in angle brackets< to denote the generic type Later, when a client uses our code, the client can substitute an actual type for the template parameter the client can thus obtain several actual pieces of code from our template, using different actual types in place of the template parameter
6.2 Implementations of Lists Class Templates ◆A C++ template construction allows us to write code, usually code to implement a class, that uses objects of an arbitrary, generic type. ◆In template code we utilize a parameter enclosed in angle brackets< > to denote the generic type. ◆Later, when a client uses our code, the client can substitute an actual type for the template parameter. The client can thus obtain several actual pieces of code from our template, using different actual types in place of the template parameter
Example: We shall implement a template class List that depends on one generic type parameter. A client can then use our template to declare several lists with different types of entries with declarations of the following form Listsint> rst list Listschar> second list
Example: We shall implement a template class List that depends on one generic type parameter. A client can then use our template to declare several lists with different types of entries with declarations of the following form: List<int> rst list; List<char> second list;
Templates provide a new mechanism for creating generic data structures, one that allows many different specializations of a given data structure template in a single application 连续/顺序(存储)的 The added generality that we get bw ong templates comes at the price of nightly one-dimensional arrays complicated class specifications and implementations Contiguous(storage) Implementation
Templates provide a new mechanism for creating generic data structures, one that allows many different specializations of a given data structure template in a single application. The added generality that we get by using templates comes at the price of slightly more complicated class specifications and implementations. Contiguous(storage) Implementation 连续/顺序(存储)的 One-dimensional arrays