DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No More Array Errors (Part II) - Array_alg(3C++)

Selecting an arbitrary Array_set element

The Array_set function select() selects an arbitrary element and returns a pointer to it. An overzealous interpretation of ``arbitrary'' might tempt us to return a random element. We could do this by calling random(), described in random(3C++), which returns a random pointer into an array:

       const T* select()const{
           check();
           return (n==0 ? 0 :
                     random(&b[0],&b[n]));
       }

From an efficiency standpoint, however, it is better to do as little work as possible to meet the specification. We therefore simply return the first element, if one exists:

       const T* select()const{
           check();
           return (n==0 ? 0 : &b[0]);
       }

Next topic: Array_set algebra
Previous topic: Array_set relations

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004