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

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(C++), 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

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005