| | | | | |

Testing Generic Algorithms

/*  fga.cpp

    functionality test of generic algorithms using
    List<T>, Vector<T>, Deque<T>, and arrays of T
*/

typedef char ElementType; const char* e_t = "char";

List      < ElementType > L;  // a list
Vector    < ElementType > V;  // a vector
Deque     < ElementType > Q;  // a deque
ElementType *              A;  // an array

List      < ElementType >::Iterator Litr, Lloc;
Vector    < ElementType >::Iterator Vitr, Vloc;
Deque     < ElementType >::Iterator Qitr, Qloc;
ElementType                       *  Aitr, * Aloc;

ElementType e, s; // entry, sentinal
unsigned int i, p, size;

// define the list L somehow, and properly size all containers
// ...

// copy to vector, deque, and array
g_copy (L.Begin(), L.End(), V.Begin());
g_copy (V.Begin(), V.End(), Q.Begin());
g_copy (Q.Begin(), Q.End(), A);
// ...
 
// g_min_element
Lloc = g_min_element(L.Begin(), L.End());
Vloc = g_min_element(V.Begin(), V.End());
Qloc = g_min_element(Q.Begin(), Q.End());
Aloc = g_min_element(A, A + size);
// ...

// g_max_element
Lloc = g_max_element(L.Begin(), L.End());
Vloc = g_max_element(V.Begin(), V.End());
Qloc = g_max_element(Q.Begin(), Q.End());
Aloc = g_max_element(A, A + size);

// g_find (sequential search each container for value e):
Lloc = g_find (L.Begin(), L.End(), e);
Vloc = g_find (V.Begin(), V.End(), e);
Qloc = g_find (Q.Begin(), Q.End(), e);
Aloc = g_find (A, A + size, e);

// g_for_each (apply a SmartMakeUpperCase object to each element of each container):
SmartMakeUpperCase smuc;
smuc = g_for_each(L.Begin(), L.End(), smuc);
smuc = g_for_each(V.Begin(), V.End(), smuc);
smuc = g_for_each(Q.Begin(), Q.End(), smuc);
smuc g_for_each(A, A + size, smuc);

// output the number of conversions from lower case to upper case:
std::cout << "Number of case conversions: " << smuc.count;

| | Top of Page | 8. Generic Algorithms - 8 of 12