home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 3 / stl.exe / stl_ex.cpp < prev    next >
C/C++ Source or Header  |  1998-02-09  |  83KB  |  2,692 lines

  1. //---------------------------------------------------------------------------
  2. // Borland C++Builder
  3. // Copyright (c) 1987, 1998 Borland International Inc.  All Rights Reserved.
  4. //---------------------------------------------------------------------------
  5. /* Standard C++ library examples */
  6.  
  7. #include "std1.h"
  8. #include <algorithm>
  9. #include <bitset>
  10. #include <complex>
  11. #include <ctype.h>
  12. #include <deque>
  13. #include <fstream.h>
  14. #include <functional>
  15. #include <iomanip.h>
  16. #include <iostream.h>
  17. #include <iterator>
  18. #include <limits>
  19. #include <list>
  20. #include <map>
  21. #include <memory>
  22. #include <numeric>
  23. #include <queue>
  24. #include <set>
  25. #include <stack>
  26. #include <stdexcept>
  27. #include <string.h>
  28. #include <string>
  29. #include <strstrea.h>
  30. #include <utility>
  31. #include <vector>
  32.  
  33. using namespace std;
  34.  
  35. extern int ct;  // counter for current memo line
  36.  
  37.  int accum_ex () /* accum */
  38.  {
  39.    //
  40.    // Typedef for vector iterators.
  41.    //
  42.    Form1->Memo1->Lines->Strings[ct++] = " ========== Accumulator Example =========";
  43.    typedef vector<int>::iterator iterator;
  44.    //
  45.    // Initialize a vector using an array of integers.
  46.    //
  47.    int d1[10] = {1,2,3,4,5,6,7,8,9,10};
  48.    vector<int> v1(d1+0, d1+10);
  49.    //
  50.    // Accumulate sums and products.
  51.    //
  52.    int sum  = accumulate(v1.begin(), v1.end(), 0);
  53.    //
  54.    // Output the results.
  55.    //
  56.    Form1->Memo1->Lines->Strings[ct++] = "For the series: ";
  57.    for(iterator i = v1.begin(); i != v1.end(); i++)
  58.       Form1->Memo1->Lines->Strings[ct] =
  59.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*i) + " ";
  60.    ct++;
  61.    Form1->Memo1->Lines->Strings[ct++] = " where N = 10.";
  62.    Form1->Memo1->Lines->Strings[ct++] = "The sum = (N*N + N)/2 = " +IntToStr(sum);
  63.  
  64.    return 0;
  65.  }
  66.  
  67.  
  68.  int advance_ex () /* advance */
  69.  
  70.  {
  71.    //
  72.    // Initialize a list using an array.
  73.    //
  74.    Form1->Memo1->Lines->Strings[ct++] = " ============= Advance Example ==========";
  75.    int arr[6] = {3,4,5,6,7,8};
  76.    list<int> l(arr+0, arr+6);
  77.    //
  78.    // Declare a list iterator, s.b. a ForwardIterator.
  79.    //
  80.    list<int>::iterator itr = l.begin();
  81.    //
  82.    // Output the original list.
  83.    //
  84.    Form1->Memo1->Lines->Strings[ct++] = "For the list: ";
  85.    for( itr = l.begin(); itr != l.end(); itr++)
  86.       Form1->Memo1->Lines->Strings[ct] =
  87.            Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  88.    ct++;
  89.    Form1->Memo1->Lines->Strings[ct++] = "When the iterator is initialized to l.begin(),";
  90.    Form1->Memo1->Lines->Strings[ct++] = " it points to "+IntToStr(*itr);
  91.    //
  92.    // operator+ is not available for a ForwardIterator, so use advance.
  93.    //
  94.    advance(itr, 4);
  95.    Form1->Memo1->Lines->Strings[ct++] = "After advance(itr,4), the iterator points to "+IntToStr(*itr);
  96.  
  97.    return 0;
  98.   }
  99.  
  100.  int adj_diff_ex () /* adj_diff */
  101.  {
  102.    //
  103.    // Initialize a vector of ints from an array.
  104.    //
  105.    typedef vector<int>::iterator iterator;
  106.    Form1->Memo1->Lines->Strings[ct++] = " ========== Adj_difference Example ======";
  107.    int arr[10] = {1,1,2,3,5,8,13,21,34,55};
  108.    vector<int> v(arr+0, arr+10);
  109.    //
  110.    // Two uninitialized vectors for storing results.
  111.    //
  112.    vector<int> diffs(10);
  113.    //
  114.    // Calculate difference(s) using default operator (minus).
  115.    //
  116.    adjacent_difference(v.begin(),v.end(),diffs.begin());
  117.    //
  118.    // Output the results.
  119.    //
  120.    Form1->Memo1->Lines->Strings[ct++] = "For the vector: " ;
  121.    for(iterator itr = v.begin(); itr != v.end(); itr++)
  122.       Form1->Memo1->Lines->Strings[ct] =
  123.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  124.    ct++;
  125.    Form1->Memo1->Lines->Strings[ct++] = "The differences between adjacent elements are: ";
  126.    for(iterator itr = diffs.begin(); itr != diffs.end(); itr++)
  127.       Form1->Memo1->Lines->Strings[ct] =
  128.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  129.    ct++;
  130.    return 0;
  131.  }
  132.  
  133. // *** start alg1 source
  134.  
  135. class iotaGen {
  136. public:
  137.     iotaGen (int iv) : current(iv) { }
  138.     operator () () { return current++; }
  139. private:
  140.     int current;
  141. };
  142.  
  143. void fill_example ()
  144.     // illustrate the use of the fill and fill_n functions
  145. {
  146.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating fill function:" ;
  147.         // example 1, fill an array with initial values
  148.     char buffer[100], *bufferp = buffer;
  149.     fill(bufferp, bufferp + 100, '\0');
  150.     fill_n(bufferp, 10, 'x');
  151.     Form1->Memo1->Lines->Strings[ct++] = buffer ;
  152.  
  153.         // example 2, use fill to initialize a list
  154.     list<string> aList;
  155.     list<string> ResultList;
  156.     list<string>::iterator listItr;
  157.  
  158.     fill_n (inserter(aList, aList.begin()), 10, "empty");
  159.     copy(aList.begin(), aList.end(), inserter(ResultList,ResultList.begin()));
  160.     for(listItr = ResultList.begin(); listItr != ResultList.end(); listItr++)
  161.          Form1->Memo1->Lines->Strings[ct] =
  162.           Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
  163.     ct++;
  164.  
  165.         // example 3, use fill to overwrite values in a list
  166.     fill (aList.begin(), aList.end(), "full");
  167.     for(listItr = aList.begin(); listItr != aList.end(); listItr++)
  168.          Form1->Memo1->Lines->Strings[ct] =
  169.           Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
  170.     ct++;
  171.  
  172.         // example 4, fill in a portion of a list
  173.     vector<int> iVec(10);
  174.     vector<int>::iterator vItr;
  175.     generate (iVec.begin(), iVec.end(), iotaGen(1));
  176.     vector<int>::iterator seven = find(iVec.begin(), iVec.end(), 7);
  177.     fill(iVec.begin(), seven, 0);
  178.     for(vItr = iVec.begin(); vItr != iVec.end(); vItr++)
  179.          Form1->Memo1->Lines->Strings[ct] =
  180.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  181.     ct++;
  182. }
  183.  
  184. void copy_example()
  185.     // illustrate the use of the copy function
  186. {
  187.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating copy function: " ;
  188.     char * source = "reprise";
  189.     char * surpass = "surpass";
  190.     char buffer[120], *bufferp = buffer;
  191.     vector<char>::iterator listItr;
  192.  
  193.         // example 1, a simple copy
  194.     copy(source, source + strlen(source) + 1, bufferp);
  195.  
  196.         // example 2, self copies
  197.     * copy(bufferp + 2, bufferp+ strlen(buffer), bufferp) = '\0';
  198.     int buflen = strlen(buffer) + 1;
  199.     copy_backward(bufferp, bufferp + buflen, bufferp + buflen + 3);
  200.     copy(surpass, surpass + 3, bufferp);
  201.  
  202.     for(listItr = bufferp; listItr < bufferp + strlen(buffer); listItr++)
  203.          Form1->Memo1->Lines->Strings[ct] =
  204.           Form1->Memo1->Lines->Strings[ct] + *listItr;
  205.     ct++;
  206.  
  207.         // example 4, use copy to convert type
  208.     list<char> char_list;
  209.     list<char>::iterator listItr2;
  210.     copy(bufferp, bufferp + strlen(buffer),inserter(char_list,char_list.end()));
  211.     char * big = "big ";
  212.     copy(big, big + 4, inserter(char_list, char_list.begin()));
  213.     for(listItr2 = char_list.begin(); listItr2 != char_list.end(); listItr2++)
  214.          Form1->Memo1->Lines->Strings[ct] =
  215.           Form1->Memo1->Lines->Strings[ct] + *listItr2;
  216.     ct++;
  217.  
  218.     char /*buffer2[200],*/ *buffer2p = buffer;
  219.     * copy(char_list.begin(), char_list.end(), buffer2p) = '\0';
  220.     Form1->Memo1->Lines->Strings[ct++] = buffer2p ;
  221. }
  222.  
  223. # include <strstrea.h>
  224.  
  225. string generateLabel() {
  226.     // generate a label string of the form L_ddd
  227.     static int lastLabel = 0;
  228.     char labelBuffer[80];
  229.     ostrstream ost(labelBuffer, 80);
  230.     ost << "L_" << lastLabel++ << '\0';
  231.     return string(labelBuffer);
  232. }
  233.  
  234. void generate_example ()
  235.     // illustrate the use of the generate and genrate_n functions
  236. {
  237.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating generate algorithm:" ;
  238.  
  239.         // example 1, generate a list of label numbers
  240.     list<string> labelList;
  241.     list<string>::iterator listItr;
  242.     generate_n (inserter(labelList, labelList.begin()), 4, generateLabel);
  243.     for(listItr = labelList.begin(); listItr != labelList.end(); listItr++)
  244.          Form1->Memo1->Lines->Strings[ct] =
  245.           Form1->Memo1->Lines->Strings[ct] + (String)(*listItr).data() + " ";
  246.     ct++;
  247.  
  248.         // example 2, generate an arithmetic progression
  249.     vector<int> iVec(10);
  250.     vector<int>::iterator vItr;
  251.     generate (iVec.begin(), iVec.end(), iotaGen(2));
  252.     generate_n (iVec.begin(), 5, iotaGen(7));
  253.     for(vItr = iVec.begin(); vItr != iVec.end(); vItr++)
  254.          Form1->Memo1->Lines->Strings[ct] =
  255.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  256.     ct++;
  257. }
  258.  
  259. void swap_example ()
  260.     // illustrate the use of the algorithm swap_ranges
  261. {
  262.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating swap_ranges algorithm:" ;
  263.  
  264.         // first make two parallel sequences
  265.     int data[] = {12, 27, 14, 64}, *datap = data;
  266.     vector<int> aVec(4);
  267.     vector<int>::iterator vItr;
  268.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  269.  
  270.         // illustrate swap and swap_itr
  271.     swap(data[0], data[2]);
  272.     for(vItr = data; vItr != data+4; vItr++)
  273.          Form1->Memo1->Lines->Strings[ct] =
  274.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  275.     ct++;
  276.     vector<int>::iterator last = aVec.end(); last--;
  277.     iter_swap(aVec.begin(), last);
  278.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  279.          Form1->Memo1->Lines->Strings[ct] =
  280.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  281.     ct++;
  282.  
  283.         // now swap the entire sequence
  284.     swap_ranges (aVec.begin(), aVec.end(), datap);
  285.     for(vItr = data; vItr != data+4; vItr++)
  286.          Form1->Memo1->Lines->Strings[ct] =
  287.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  288.     ct++;
  289.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  290.          Form1->Memo1->Lines->Strings[ct] =
  291.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  292.     ct++;
  293. }
  294.  
  295. int alg1_ex() /* alg1 */
  296. {
  297.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- initialization ";
  298.     fill_example();
  299.     copy_example();
  300.     generate_example();
  301.     swap_example();
  302.  
  303.     Form1->Memo1->Lines->Strings[ct++] = "End of initialization tests" ;
  304.     return 0;
  305. }
  306.  
  307.  
  308. // *** start alg2 source
  309.  
  310. int randomInteger(int m)
  311. {    return rand() % m; }
  312.  
  313. bool isLeapYear (unsigned int year)
  314. {
  315.     if (year % 1000 == 0)
  316.         return true;
  317.     if (year % 100 == 0)
  318.         return false;
  319.     if (year % 4 == 0)
  320.         return true;
  321.     return false;
  322. }
  323.  
  324. bool operator < (const string & left, const string & right)
  325. {    return left.compare(right) < 0; }
  326.  
  327. void split
  328.     (const string & text, const string & separators, list<string> & words)
  329. {
  330.     int n = text.length();
  331.     int start, stop;
  332.  
  333.     start = text.find_first_not_of(separators);
  334.     while ((start >= 0) && (start < n)) {
  335.         stop = text.find_first_of(separators, start);
  336.         if ((stop < 0) || (stop > n)) stop = n;
  337.         words.push_back (text.substr(start, stop-start));
  338.         start = text.find_first_not_of(separators, stop+1);
  339.         }
  340. }
  341.  
  342.  
  343. void find_test()
  344.     // illustrate use of the find function
  345. {
  346.     Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm find:" ;
  347.     int vintageYears[] = {1967, 1972, 1974, 1980, 1995};
  348.  
  349.     vector<int>::iterator start = vintageYears;
  350.     vector<int>::iterator stop = vintageYears + 5;
  351.     
  352.     vector<int>::iterator where = find_if(start, stop, isLeapYear);
  353.  
  354.     if (where != stop)
  355.         Form1->Memo1->Lines->Strings[ct++] = "first vintage leap year is " + IntToStr(*where) ;
  356.     else
  357.         Form1->Memo1->Lines->Strings[ct++] = "no vintage leap years" ;
  358.  
  359.     where = find(start, stop, 1995);
  360.  
  361.     if (where != stop)
  362.         Form1->Memo1->Lines->Strings[ct++] = "1995 is position " + IntToStr(where - start) + " in sequence" ;
  363.     else
  364.         Form1->Memo1->Lines->Strings[ct++] = "1995 does not occur in sequence" ;
  365.  
  366. }
  367.  
  368. void find_adjacent_test()
  369. {
  370.     Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm find adjacent" ;
  371.     char * text = "The bookkeeper carefully opened the door";
  372.     
  373.     vector<char>::iterator start = text;
  374.     vector<char>::iterator stop = text + strlen(text);
  375.     
  376.     vector<char>::iterator where = start;
  377.  
  378.     Form1->Memo1->Lines->Strings[ct++] = "In the text " + (String)text ;
  379.     while ((where = adjacent_find(where, stop)) != stop) {
  380.         Form1->Memo1->Lines->Strings[ct++] = "double " + IntToStr(*where) + " in position " + IntToStr(where - start) ;
  381.         ++where;
  382.         }
  383. }
  384.  
  385. void search_test()
  386.     // illustrate the use of the search function
  387. {
  388.     Form1->Memo1->Lines->Strings[ct++] = "Test of algorithm search:" ;
  389.     char * base = "aspirations";
  390.     char * text = "ration";
  391.     
  392.     char * where = search(base, base + strlen(base), text, text + strlen(text));
  393.  
  394.     if (*where != '\0')
  395.         Form1->Memo1->Lines->Strings[ct++] = "substring begins in position " << IntToStr(where - base) ;
  396.     else
  397.         Form1->Memo1->Lines->Strings[ct++] = "substring does not occur in text" ;
  398. }
  399.  
  400. void max_min_example ()
  401.     // illustrate use of max_element and min_element algorithms
  402. {
  403.     string MyString;
  404.  
  405.     Form1->Memo1->Lines->Strings[ct++] = "Test of max and min algorithms: " ;
  406.     // make a vector of random numbers between 0 and 99
  407.     vector<int> numbers(25);
  408.     for (int i = 0; i < 25; i++)
  409.         numbers[i] = randomInteger(100);
  410.  
  411.     // print the maximum
  412.     vector<int>::iterator max =
  413.         max_element(numbers.begin(), numbers.end());
  414.     Form1->Memo1->Lines->Strings[ct++] = "largest value was " + IntToStr(*max) ;
  415.  
  416.         // example using strings
  417.     string text =
  418.         "it was the best of times, it was the worst of times.";
  419.     list<string>words;
  420.     split(text, " .,!:;", words);
  421.     MyString =  (* min_element(words.begin(), words.end()));
  422.     Form1->Memo1->Lines->Strings[ct] = "The smallest word is ";
  423.     Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct]+(String)MyString.data();
  424.     ct++;
  425.     MyString =  (* max_element(words.begin(), words.end()));
  426.     Form1->Memo1->Lines->Strings[ct++] =  "and the largest word is "+(String)MyString.data();
  427. }
  428.  
  429. void mismatch_test(char * a, char * b)
  430.     // illustrate the use of the mismatch function
  431. {
  432.     pair<char *, char *> differPositions(0, 0);
  433.     char * aDiffPos;
  434.     char * bDiffPos;
  435.  
  436.     if (strlen(a) < strlen(b)) {
  437.         differPositions = mismatch(a, a + strlen(a), b);
  438.         aDiffPos = differPositions.first;
  439.         bDiffPos = differPositions.second;
  440.         }
  441.     else {
  442.         differPositions = mismatch(b, b + strlen(b), a);
  443.         aDiffPos = differPositions.second;
  444.         bDiffPos = differPositions.first;
  445.         }
  446.  
  447.     Form1->Memo1->Lines->Strings[ct++] = "string " + (String)a;
  448.     if (*aDiffPos == *bDiffPos)
  449.         Form1->Memo1->Lines->Strings[ct] = " is equal to ";
  450.     else if (*aDiffPos < *bDiffPos)
  451.         Form1->Memo1->Lines->Strings[ct] = " is less than ";
  452.     else
  453.         Form1->Memo1->Lines->Strings[ct] = " is greater than ";
  454.     Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + String(b) ;
  455.     ct++;
  456. }
  457.  
  458. int alg2_ex() /* alg2 */
  459. {
  460.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- Searching " ;
  461.     find_test();
  462.     find_adjacent_test();
  463.     search_test;
  464.     max_min_example();
  465.     mismatch_test("goody", "goody");
  466.     mismatch_test("good", "goody");
  467.     mismatch_test("goody", "good");
  468.     mismatch_test("good", "fred");
  469.     mismatch_test("fred", "good");
  470.  
  471.     Form1->Memo1->Lines->Strings[ct++] = "End of search algorithms test program" ;
  472.     return 0;
  473. }
  474.  
  475.  
  476. // *** start alg3 source 
  477.  
  478. class iotaGenerator {
  479. public:
  480.     iotaGenerator (int iv) : current(iv) { }
  481.     operator () () { return current++; }
  482. private:
  483.     int current;
  484. };
  485.  
  486. bool isEven(int n) { return 0 == (n % 2); }
  487.  
  488. void reverse_example ()
  489.     // illustrate the use of the reverse function
  490. {
  491.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating reverse algorithm:" ;
  492.     
  493.         // example 1, reversing a string
  494.     char * text = "Rats live on no evil star";
  495.     reverse (text, text + strlen(text));
  496.     Form1->Memo1->Lines->Strings[ct++] = text ;
  497.  
  498.         // example 2, reversing a list
  499.     list<int> iList;
  500.     list<int>::iterator lItr;
  501.     generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(2));
  502.     reverse (iList.begin(), iList.end());
  503.     for(lItr = iList.begin(); lItr != iList.end(); lItr++)
  504.          Form1->Memo1->Lines->Strings[ct] =
  505.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  506.     ct++;
  507. }
  508.  
  509. void replace_example ()
  510.     // illustrate the use of the replace function
  511. {
  512.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating replace algorithm:" ;
  513.  
  514.         // make vector 0 1 2 3 4
  515.     vector<int> numbers(11);
  516.     vector<int>::iterator vItr;
  517.     for (int i = 0; i < 11; i++)
  518.         numbers[i] = i < 5 ? i : 10 - i;
  519.     for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
  520.          Form1->Memo1->Lines->Strings[ct] =
  521.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  522.     ct++;
  523.  
  524.  
  525.         // replace 0 by 2
  526.     replace (numbers.begin(), numbers.end(), 3, 7);
  527.     for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
  528.          Form1->Memo1->Lines->Strings[ct] =
  529.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  530.     ct++;
  531.  
  532.         // replace even numbers by 9
  533.     replace_if (numbers.begin(), numbers.end(), isEven, 9);
  534.     for(vItr = numbers.begin(); vItr != numbers.end(); vItr++)
  535.          Form1->Memo1->Lines->Strings[ct] =
  536.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  537.     ct++;
  538.  
  539.         // copy into a list, replacing 9 by 3
  540.     int aList[] = {2, 1, 4, 3, 2, 5};
  541.     int bList[6];
  542.     int cList[6];
  543.     replace_copy (aList, aList+6, &bList[0], 2, 7);
  544.     replace_copy_if (bList, bList+6, &cList[0], bind2nd(greater<int>(), 3), 8);
  545.     for(vItr = bList; vItr < bList+6; vItr++)
  546.          Form1->Memo1->Lines->Strings[ct] =
  547.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  548.     ct++;
  549.     for(vItr = cList; vItr < cList+6; vItr++)
  550.          Form1->Memo1->Lines->Strings[ct] =
  551.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  552.     ct++;
  553. }
  554.  
  555. void rotate_example ()
  556.     // illustrate the use of the rotate function
  557. {
  558.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating rotate algorithm:" ;
  559.  
  560.         // create the list 1 2 3 ... 10
  561.     list<int> iList;
  562.     list<int>::iterator lItr;
  563.     generate_n(inserter(iList, iList.begin()), 10, iotaGenerator(1));
  564.  
  565.         // find the location of the seven
  566.     list<int>::iterator  middle = find(iList.begin(), iList.end(), 7);
  567.  
  568.         // now rotate around that location
  569.     rotate(iList.begin(), middle, iList.end());
  570.     for(lItr = iList.begin(); lItr != iList.end(); lItr++)
  571.          Form1->Memo1->Lines->Strings[ct] =
  572.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  573.     ct++;
  574.  
  575.         // rotate again around the same location
  576.     list<int> cList;
  577.     rotate_copy(iList.begin(), middle, iList.end(),
  578.         inserter(cList, cList.begin()));
  579.     for(lItr = cList.begin(); lItr != cList.end(); lItr++)
  580.          Form1->Memo1->Lines->Strings[ct] =
  581.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  582.     ct++;
  583. }
  584.  
  585.  
  586. void partition_example ()
  587.     // illustrate the use of the paration function
  588. {
  589.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating partition algorithm:" ;
  590.  
  591.         // first make the vector 1 2 3 ... 10
  592.     vector<int> numbers(10);
  593.     vector<int>::iterator vItr;
  594.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  595.  
  596.         // now put the odd values low, even values high
  597.     vector<int>::iterator result = partition (numbers.begin(), numbers.end(), isEven);
  598.     for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
  599.          Form1->Memo1->Lines->Strings[ct] =
  600.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  601.     ct++;
  602.     Form1->Memo1->Lines->Strings[ct++] = "middle location " + IntToStr(result - numbers.begin()) ;
  603.  
  604.         // now do a stable partition
  605.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  606.     for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
  607.          Form1->Memo1->Lines->Strings[ct] =
  608.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  609.     ct++;
  610. }
  611.  
  612.  
  613. bool nameCompare(char * a, char * b) { return strcmp(a, b) <= 0; }
  614.  
  615. void permutation_example ()
  616.     // illustrate the use of the next_permutation function
  617. {
  618.         // start with the values 1 2 3 in sequence
  619.     int start [] = {1, 2, 3 };
  620.     vector<int>::iterator vItr;
  621.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating permutation: " ;
  622.     do
  623.     {
  624.        for(vItr = start; vItr < start+3; vItr++)
  625.          Form1->Memo1->Lines->Strings[ct] =
  626.            Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  627.        ct++;
  628.     }
  629.      while (next_permutation(start, start + 3));
  630.  
  631.     char * names[] = {"Alpha", "Beta", "Gamma"};
  632.     vector<char *>::iterator cItr;
  633.     do
  634.     {
  635.        for(cItr = names; cItr < names + 3; cItr++)
  636.          Form1->Memo1->Lines->Strings[ct] =
  637.            Form1->Memo1->Lines->Strings[ct] + *cItr + " ";
  638.        ct++;
  639.     }
  640.     while (next_permutation(names, names + 3, nameCompare));
  641.  
  642.     char * word = "bela";
  643.     do
  644.         Form1->Memo1->Lines->Strings[ct] =  Form1->Memo1->Lines->Strings[ct]+ word + " ";
  645.     while (prev_permutation(word, &word[4]));
  646.     ct++;
  647. }
  648.  
  649. void inplace_merge_example ()
  650.     // illustrate the use of the inplace_merge function
  651. {
  652.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating inplace merge algorithm" ;
  653.  
  654.         // first generate the numbers 0 2 4 6 8 1 3 5 7 9
  655.     vector<int> numbers(10);
  656.     vector<int>::iterator vItr;
  657.     for (int i = 0; i < 10; i++)
  658.         numbers[i] = i < 5 ? 2 * i : 2 * (i - 5) + 1;
  659.     for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
  660.          Form1->Memo1->Lines->Strings[ct] =
  661.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  662.     ct++;
  663.     vector<int>::iterator midvec = find(numbers.begin(), numbers.end(), 1);
  664.  
  665.         // copy them into a list
  666.     list<int> numList;
  667.     list<int>::iterator lItr;
  668.     copy(numbers.begin(), numbers.end(),
  669.             inserter(numList, numList.begin()));
  670.     list<int>::iterator midList = find(numList.begin(), numList.end(), 1);
  671.     for(lItr = numList.begin(); lItr != numList.end(); lItr++)
  672.          Form1->Memo1->Lines->Strings[ct] =
  673.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  674.     ct++;
  675.  
  676.         // now put them back together
  677.     inplace_merge(numbers.begin(), midvec, numbers.end());
  678.     inplace_merge(numList.begin(), midList, numList.end());
  679.     for(lItr = numList.begin(); lItr != numList.end(); lItr++)
  680.          Form1->Memo1->Lines->Strings[ct] =
  681.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  682.     ct++;
  683. }
  684.  
  685. struct RandomInteger
  686. {
  687.     operator() (int m) { return rand() % m; }
  688. };
  689.  
  690. void random_shuffle_example()
  691.     // illustrate the use of the random_shuffle function
  692. {
  693.         // first make the vector 1 2 3 ... 10
  694.     vector<int> numbers(10);
  695.     vector<int>::iterator vItr;
  696.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating random shuffle:" ;
  697.     generate(numbers.begin(), numbers.end(), iotaGenerator(1));
  698.  
  699.     RandomInteger random;
  700.  
  701.         // randomly shuffle the elements
  702.     random_shuffle(numbers.begin(), numbers.end(), random);
  703.     for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
  704.          Form1->Memo1->Lines->Strings[ct] =
  705.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  706.     ct++;
  707.  
  708.         // do it again
  709.     random_shuffle(numbers.begin(), numbers.end(), random);
  710.     for(vItr = numbers.begin(); vItr < numbers.end(); vItr++)
  711.          Form1->Memo1->Lines->Strings[ct] =
  712.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  713.     ct++;
  714. }
  715.  
  716. int alg3_ex() /* alg3 */
  717. {
  718.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- in-place " ;
  719.  
  720.     reverse_example();
  721.     replace_example();
  722.     rotate_example();
  723.     partition_example();
  724.     permutation_example();
  725.     inplace_merge_example();
  726.     random_shuffle_example();
  727.     
  728.     Form1->Memo1->Lines->Strings[ct++] = "End of in-place algorithm sample program"  ;
  729.  
  730.     return 0;
  731. }
  732.  
  733. // *** start alg4 source
  734.  
  735.  
  736. void remove_example ()
  737.     // illustrate the use of the remove algorithm
  738. {
  739.     Form1->Memo1->Lines->Strings[ct++] = "Remove Algorithm examples:" ;
  740.  
  741.     // create a list of numbers
  742.     int data[] = {1, 2, 4, 3, 1, 4, 2};
  743.     list<int> aList;
  744.     list<int>::iterator itr = aList.begin();
  745.     copy (data, data+7, inserter(aList, aList.begin()));
  746.     Form1->Memo1->Lines->Strings[ct++] = "Original list: ";
  747.     for(itr = aList.begin(); itr != aList.end(); itr++)
  748.          Form1->Memo1->Lines->Strings[ct] =
  749.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  750.  
  751.         // remove 2's, copy into a new list
  752.     ct++;
  753.     list<int> newList;
  754.     remove_copy (aList.begin(), aList.end(), back_inserter(newList), 2);
  755.     Form1->Memo1->Lines->Strings[ct++] = "After removing 2's: ";
  756.     for(itr = newList.begin(); itr != newList.end(); itr++)
  757.          Form1->Memo1->Lines->Strings[ct] =
  758.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  759.     ct++;
  760.         // remove 2's in place
  761.     list<int>::iterator where;
  762.     where = remove(aList.begin(), aList.end(), 2);
  763.     Form1->Memo1->Lines->Strings[ct++] = "List after removal, before erase: ";
  764.     for(itr = aList.begin(); itr != aList.end(); itr++)
  765.          Form1->Memo1->Lines->Strings[ct] =
  766.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  767.     ct++;
  768.     aList.erase(where, aList.end());
  769.     Form1->Memo1->Lines->Strings[ct++] = "List after erase: ";
  770.     for(itr = aList.begin(); itr != aList.end(); itr++)
  771.          Form1->Memo1->Lines->Strings[ct] =
  772.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  773.     ct++;
  774.  
  775.         // remove all even values
  776.     where = remove_if (aList.begin(), aList.end(), isEven);
  777.     aList.erase(where, aList.end());
  778.     Form1->Memo1->Lines->Strings[ct++] = "List after removing even values: ";
  779.     for(itr = aList.begin(); itr != aList.end(); itr++)
  780.          Form1->Memo1->Lines->Strings[ct] =
  781.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  782.     ct++;
  783.  
  784. }
  785.  
  786. void unique_example ()
  787.     // illustrate use of the unqiue algorithm
  788. {
  789.         // first make a list of values
  790.     int data[] = {1, 3, 3, 2, 2, 4};
  791.     list<int> aList;
  792.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating unique algorithm" ;
  793.     copy(data, data+6, inserter(aList, aList.begin()));
  794.     list<int>::iterator itr = aList.begin();
  795.     Form1->Memo1->Lines->Strings[ct++] = "Original List: ";
  796.     for(itr = aList.begin(); itr != aList.end(); itr++)
  797.          Form1->Memo1->Lines->Strings[ct] =
  798.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  799.     ct++;
  800.  
  801.         // copy unique elements into a set
  802.     set<int, less<int> > aSet;
  803.     set<int, less<int> >::iterator s_itr = aSet.begin();
  804.     unique_copy(aList.begin(), aList.end(), inserter(aSet, aSet.begin()));
  805.     Form1->Memo1->Lines->Strings[ct++] = "Set after unique_copy: ";
  806.     for(s_itr = aSet.begin(); s_itr != aSet.end(); s_itr++)
  807.          Form1->Memo1->Lines->Strings[ct] =
  808.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*s_itr) + " ";
  809.     ct++;
  810.  
  811.         // copy unique elements in place
  812.     list<int>::iterator where;
  813.     where = unique(aList.begin(), aList.end());
  814.     Form1->Memo1->Lines->Strings[ct++] = "List after calling unique: ";
  815.     for(itr = aList.begin(); itr != aList.end(); itr++)
  816.          Form1->Memo1->Lines->Strings[ct] =
  817.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  818.     ct++;
  819.  
  820.         // remove trailing values
  821.     aList.erase(where, aList.end());
  822.     Form1->Memo1->Lines->Strings[ct++] = "List after erase: ";
  823.     for(itr = aList.begin(); itr != aList.end(); itr++)
  824.          Form1->Memo1->Lines->Strings[ct] =
  825.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  826.     ct++;
  827. }
  828.  
  829. int alg4_ex() { /* alg4 */
  830.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- Removal " ;
  831.     remove_example();
  832.     unique_example();
  833.  
  834.     cout << "End of removal algorithms sample program" ;
  835.     return 0;
  836. }
  837.  
  838. // *** start alg5 source
  839.  
  840. // prototypes used to halt warnings
  841. bool isVowel (char);
  842. void count_example();
  843. void accumulate_example();
  844. template<class T>
  845. list<T> & listadd(list<T> & base, T & newValue);
  846. void inner_product_example();
  847. void equal_example();
  848.  
  849.  
  850. bool isVowel (char c)
  851. {
  852.     switch (c) {
  853.         case 'a': case 'A':
  854.         case 'e': case 'E':
  855.         case 'i': case 'I':
  856.         case 'o': case 'O':
  857.         case 'u': case 'U':
  858.             return true;
  859.         }
  860.     return false;
  861. }
  862.  
  863. void count_example()
  864.     // illustrate the use of the count function
  865. {
  866.     int ecount = 0;
  867.     int vowelCount = 0;
  868.     
  869.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating count function:" ;
  870.     char * text = "Now is the time to begin";
  871.     
  872.     count (text, text + strlen(text), 'e', ecount);
  873.     count_if (text, text + strlen(text), isVowel, vowelCount);
  874.  
  875.     Form1->Memo1->Lines->Strings[ct++] = "There are " + IntToStr(ecount) + " letter e's "  + "and "
  876.         + IntToStr(vowelCount) + " vowels in the text:" + text ;
  877. }
  878.  
  879. list<int>& intReplicate (list<int>& nums, int n)
  880.     // add n to 1 to list
  881. {
  882.     while (n) nums.push_back(n--);
  883.     return nums;
  884. }
  885.  
  886. void accumulate_example()
  887.     // illustrate the use of the accumulate function
  888. {
  889.     int numbers[] = {1, 2, 3, 4, 5};
  890.  
  891.     int sum = accumulate(numbers, numbers+5, 0);
  892.  
  893.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating accumulate function:" ;
  894.     Form1->Memo1->Lines->Strings[ct++] = "The sum of the first five numbers is " + IntToStr(sum) ;
  895.  
  896.         // example with different types for init
  897.     list<int> nums;
  898.     list<int>::iterator itr = nums.begin();
  899.     nums = accumulate(numbers, numbers+5, nums, intReplicate);
  900.     for(itr = nums.begin(); itr != nums.end(); itr++)
  901.          Form1->Memo1->Lines->Strings[ct] =
  902.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  903.     ct++;
  904. }
  905.  
  906. void inner_product_example()
  907.     // illustrate the use of the inner_product function
  908. {
  909.     int a[] = {4, 3, -2};
  910.     int b[] = {7, 3, 2};
  911.  
  912.         // example 1, simple inner product
  913.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating inner product:" ;
  914.     int in1 = inner_product(a, a+3, b, 0);
  915.     Form1->Memo1->Lines->Strings[ct++] = "Inner product is " + IntToStr(in1) ;
  916.  
  917.         // example 2, using different operations
  918.     bool anyequal = inner_product(a, a+3, b, true, logical_or<bool>(), equal_to<int>());
  919.     Form1->Memo1->Lines->Strings[ct++] = "any equal? " + IntToStr(anyequal) ;
  920. }
  921.  
  922. void equal_example()
  923.     // illustrate the use of the equal function
  924. {
  925.     int a[] = {4, 5, 3};
  926.     int b[] = {4, 3, 3};
  927.     int c[] = {4, 5, 3};
  928.  
  929.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating equal function:" ;
  930.     Form1->Memo1->Lines->Strings[ct++] = "a = b is:" + IntToStr(equal(a, a+3, b)) ;
  931.     Form1->Memo1->Lines->Strings[ct++] = "a = c is:" + IntToStr(equal(a, a+3, c)) ;
  932.     Form1->Memo1->Lines->Strings[ct++] = "a pair-wise-greater_equal b is" + IntToStr(equal(a, a+3, b, greater_equal<int>()));
  933. }
  934.  
  935. void lexical_comparison_example ()
  936.     // illustrate the use of the lexical_comparison function
  937. {
  938.     char * wordOne = "everything";
  939.     char * wordTwo = "everybody";
  940.  
  941.     Form1->Memo1->Lines->Strings[ct++] = "Illustrating lexical_comparison function:" ;
  942.     Form1->Memo1->Lines->Strings[ct++] = "compare everybody to everything " +
  943.         IntToStr(lexicographical_compare(wordTwo, wordTwo + strlen(wordTwo), wordOne, wordOne + strlen(wordOne)));
  944.  
  945.     int a[] = {3, 4, 5, 2};
  946.     int b[] = {3, 4, 5};
  947.     int c[] = {3, 5};
  948.  
  949.     Form1->Memo1->Lines->Strings[ct++] = "compare a to b: " + IntToStr(lexicographical_compare(a, a+4, b, b+3) );
  950.     Form1->Memo1->Lines->Strings[ct++] = "compare a to c: " + IntToStr(lexicographical_compare(a, a+4, c, c+2) );
  951. }
  952.  
  953. int alg5_ex() { /* alg5 */
  954.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- algorithms with scalar results" ;
  955.     count_example();
  956.     accumulate_example();
  957.     inner_product_example();
  958.     equal_example();
  959.     lexical_comparison_example();
  960.     ct++;
  961.     Form1->Memo1->Lines->Strings[ct++] = "End of scalar algorithms test"  ;
  962.     return 0;
  963. }
  964.  
  965. // *** start alg6 source
  966.  
  967. int square(int n) { return n * n; }
  968.  
  969. void transform_example ()
  970.     // illustrate the use of the transform algorithm
  971. {
  972.     // generate a list of values from 1 to 6
  973.     list<int> aList;
  974.     generate_n (inserter(aList, aList.begin()), 6, iotaGen(1));
  975.     list<int>::iterator itr = aList.begin();
  976.     Form1->Memo1->Lines->Strings[ct++] = "Original list: ";
  977.     for(itr = aList.begin(); itr != aList.end(); itr++)
  978.          Form1->Memo1->Lines->Strings[ct] =
  979.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  980.     ct++;
  981.         // transform elements by squaring, copy into vector
  982.     vector<int> aVec(6);
  983.     transform (aList.begin(), aList.end(), aVec.begin(), square);
  984.     vector<int>::iterator vitr;
  985.     Form1->Memo1->Lines->Strings[ct++] = "After squaring: ";
  986.     for(vitr = aVec.begin(); vitr != aVec.end(); vitr++)
  987.          Form1->Memo1->Lines->Strings[ct] =
  988.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  989.     ct++;
  990.  
  991.         // transform vector again, in place, yielding 4th powers
  992.     transform (aVec.begin(), aVec.end(), aVec.begin(), square);
  993.     Form1->Memo1->Lines->Strings[ct++] = "After squaring again: ";
  994.     for(vitr = aVec.begin(); vitr != aVec.end(); vitr++)
  995.          Form1->Memo1->Lines->Strings[ct] =
  996.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  997.     ct++;
  998.  
  999.         // transform in parallel, yielding cubes
  1000.     vector<int> cubes(6);
  1001.     transform (aVec.begin(), aVec.end(), aList.begin(),
  1002.         cubes.begin(), divides<int>());
  1003.     Form1->Memo1->Lines->Strings[ct++] = "After division: ";
  1004.     for(vitr = cubes.begin(); vitr != cubes.end(); vitr++)
  1005.          Form1->Memo1->Lines->Strings[ct] =
  1006.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  1007.     ct++;
  1008.  
  1009. }
  1010.  
  1011.  
  1012.  
  1013. void partial_sum_example ()
  1014.     // illustrate the use of the partial sum algorithm
  1015. {
  1016.         // generate values 1 to 5
  1017.     vector<int> aVec(5);
  1018.     vector<int> ResultVec(5);
  1019.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  1020.     vector<int>::iterator vitr;
  1021.  
  1022.         // output partial sums
  1023.     Form1->Memo1->Lines->Strings[ct++] = "Partial sums examples" ;
  1024.     Form1->Memo1->Lines->Strings[ct++] = "Partial sums : ";
  1025.     partial_sum (aVec.begin(), aVec.end(), ResultVec.begin()) ;
  1026.     for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
  1027.          Form1->Memo1->Lines->Strings[ct] =
  1028.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  1029.     ct++;
  1030. }
  1031.  
  1032. void adjacent_difference_example ()
  1033.     // illustrate the use of the adjacent difference algorithm
  1034. {
  1035.         // generate values 1 to 5
  1036.     vector<int> aVec(5);
  1037.     vector<int> ResultVec(5);
  1038.     generate (aVec.begin(), aVec.end(), iotaGen(1));
  1039.  
  1040.         // output partial sums
  1041.     Form1->Memo1->Lines->Strings[ct++] = "Adjacent Differences examples" ;
  1042.     Form1->Memo1->Lines->Strings[ct++] = "Adjacent Differences : ";
  1043.     adjacent_difference (aVec.begin(), aVec.end(),ResultVec.begin()) ;
  1044.     vector<int>::iterator vitr;
  1045.     for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
  1046.          Form1->Memo1->Lines->Strings[ct] =
  1047.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  1048.     ct++;
  1049.     //ResultVec.erase(ResultVec.begin(),ResultVec.end());
  1050.  
  1051.         // output partial products
  1052.     Form1->Memo1->Lines->Strings[ct++] = "Adjacent sums: ";
  1053.     adjacent_difference (aVec.begin(), aVec.end(),ResultVec.begin(),plus<int>()) ;
  1054.     for(vitr = ResultVec.begin(); vitr != ResultVec.end(); vitr++)
  1055.          Form1->Memo1->Lines->Strings[ct] =
  1056.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vitr) + " ";
  1057.     ct++;
  1058. }
  1059.  
  1060.  
  1061. int alg6_ex() { /* alg6 */
  1062.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms -- that transform sequences"  ;
  1063.  
  1064.     transform_example();
  1065.     partial_sum_example();
  1066.     adjacent_difference_example ();
  1067.  
  1068.     Form1->Memo1->Lines->Strings[ct++] = "End generic transform algorithms example" ;
  1069.     return 0;
  1070. }
  1071.  
  1072. // *** start alg7 source
  1073.  
  1074. int randomInteger(unsigned int n)
  1075.     { return rand() % n; }
  1076.  
  1077. int randomValue() { return randomInteger(100); }
  1078.  
  1079. void sortExample() {
  1080.     Form1->Memo1->Lines->Strings[ct++] = "Sort algorithms"  ;
  1081.  
  1082.         // fill both a vector and a deque
  1083.         // with random integers
  1084.     vector<int> aVec(15);
  1085.     vector<int>::iterator vItr;
  1086.     deque<int> aDec(15);
  1087.     deque<int>::iterator dItr;
  1088.     generate (aVec.begin(), aVec.end(), randomValue);
  1089.     generate (aDec.begin(), aDec.end(), randomValue);
  1090.     Form1->Memo1->Lines->Strings[ct++] = "Original vectors:";
  1091.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1092.          Form1->Memo1->Lines->Strings[ct] =
  1093.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1094.     ct++;
  1095.     for(dItr = aDec.begin(); dItr != aDec.end(); dItr++)
  1096.          Form1->Memo1->Lines->Strings[ct] =
  1097.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
  1098.     ct++;
  1099.     Form1->Memo1->Lines->Strings[ct++] = "-----------------------------------";
  1100.  
  1101.         // sort the vector ascending
  1102.     sort (aVec.begin(), aVec.end());
  1103.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1104.          Form1->Memo1->Lines->Strings[ct] =
  1105.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1106.     ct++;
  1107.  
  1108.         // sort the deque descending
  1109.     sort (aDec.begin(), aDec.end(), greater<int>() );
  1110.     for(dItr = aDec.begin(); dItr != aDec.end(); dItr++)
  1111.          Form1->Memo1->Lines->Strings[ct] =
  1112.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
  1113.     ct++;
  1114.  
  1115.         // sort the vector descending?
  1116.     sort (aVec.rbegin(), aVec.rend());
  1117.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1118.          Form1->Memo1->Lines->Strings[ct] =
  1119.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1120.     ct++;
  1121.  
  1122. }
  1123.  
  1124. void partial_sort_example() {
  1125.     Form1->Memo1->Lines->Strings[ct++] = "Partial sort examples" ;
  1126.  
  1127.         // make a vector of 15 random integers
  1128.     vector<int> aVec(15);
  1129.     vector<int>::iterator vItr;
  1130.     generate (aVec.begin(), aVec.end(), randomValue);
  1131.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1132.          Form1->Memo1->Lines->Strings[ct] =
  1133.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1134.     ct++;
  1135.  
  1136.  
  1137.         // partial sort the first seven positions
  1138.     partial_sort (aVec.begin(), aVec.begin() + 7, aVec.end());
  1139.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1140.          Form1->Memo1->Lines->Strings[ct] =
  1141.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1142.     ct++;
  1143.  
  1144.         // make a list of random integers
  1145.     list<int> aList(15, 0);
  1146.     list<int>::iterator lItr;
  1147.     generate (aList.begin(), aList.end(), randomValue);
  1148.     for(lItr = aList.begin(); lItr != aList.end(); lItr++)
  1149.          Form1->Memo1->Lines->Strings[ct] =
  1150.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1151.     ct++;
  1152.  
  1153.         // sort only the first seven elements
  1154.     vector<int> start(7);
  1155.     partial_sort_copy (aList.begin(), aList.end(),
  1156.         start.begin(), start.end(), greater<int>());
  1157.     for(vItr = start.begin(); vItr != start.end(); vItr++)
  1158.          Form1->Memo1->Lines->Strings[ct] =
  1159.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1160.     ct++;
  1161. }
  1162.  
  1163. void nth_element_example()
  1164.     // illustrate the use of the nth_largest function
  1165. {
  1166.     Form1->Memo1->Lines->Strings[ct++] = "Nth largest example" ;
  1167.  
  1168.         // make a vector of random integers
  1169.     vector<int> aVec(10);
  1170.     generate (aVec.begin(), aVec.end(), randomValue);
  1171.  
  1172.         // now find the 5th largest
  1173.     vector<int>::iterator nth = aVec.begin() + 4;
  1174.     nth_element(aVec.begin(), nth, aVec.end());
  1175.  
  1176.     Form1->Memo1->Lines->Strings[ct++] = "fifth largest is " + IntToStr(*nth) + " in" ;
  1177.     for(nth = aVec.begin(); nth != aVec.end(); nth++)
  1178.          Form1->Memo1->Lines->Strings[ct] =
  1179.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*nth) + " ";
  1180.     ct++;
  1181.  
  1182.  
  1183. }
  1184.  
  1185. void binary_search_example ()
  1186.     // illustrate the use of the binary search functions
  1187. {
  1188.     Form1->Memo1->Lines->Strings[ct++] = "Binary search example" ;
  1189.  
  1190.         // make an ordered vector of 15 random integers
  1191.     vector<int> aVec(15);
  1192.     vector<int>::iterator vItr;
  1193.     generate (aVec.begin(), aVec.end(), randomValue);
  1194.     sort (aVec.begin(), aVec.end());
  1195.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1196.          Form1->Memo1->Lines->Strings[ct] =
  1197.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1198.     ct++;
  1199.  
  1200.         // see if it contains an eleven
  1201.     if (binary_search(aVec.begin(), aVec.end(), 11))
  1202.         Form1->Memo1->Lines->Strings[ct++] = "contains an 11" ;
  1203.     else
  1204.         Form1->Memo1->Lines->Strings[ct++] = "does not contain an 11"  ;
  1205.  
  1206.         // insert an 11 and a 14
  1207.     vector<int>::iterator where;
  1208.     where = lower_bound (aVec.begin(), aVec.end(), 11);
  1209.     aVec.insert (where, 11);
  1210.  
  1211.     where = upper_bound (aVec.begin(), aVec.end(), 14);
  1212.     aVec.insert (where, 14);
  1213.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1214.          Form1->Memo1->Lines->Strings[ct] =
  1215.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1216.     ct++;
  1217.  
  1218. }
  1219.  
  1220. void merge_example ()
  1221.     // illustrate the use of the merge function
  1222. {
  1223.     Form1->Memo1->Lines->Strings[ct++] = "Merge algorithm examples" ;
  1224.  
  1225.         // make a list and vector of 10 random integers
  1226.     vector<int> aVec(10);
  1227.     list<int> aList(10, 0);
  1228.     list<int>::iterator lItr;
  1229.     vector<int>::iterator vItr;
  1230.  
  1231.     generate (aVec.begin(), aVec.end(), randomValue);
  1232.     sort (aVec.begin(), aVec.end());
  1233.     generate_n (aList.begin(), 10, randomValue);
  1234.     aList.sort();
  1235.     Form1->Memo1->Lines->Strings[ct++] = "Merge lists:" ;
  1236.  
  1237.     for(vItr = aVec.begin(); vItr != aVec.end(); vItr++)
  1238.          Form1->Memo1->Lines->Strings[ct] =
  1239.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1240.     ct++;
  1241.     for(lItr = aList.begin(); lItr != aList.end(); lItr++)
  1242.          Form1->Memo1->Lines->Strings[ct] =
  1243.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1244.     ct++;
  1245.         // merge into a vector
  1246.     vector<int> vResult (aVec.size() + aList.size());
  1247.     Form1->Memo1->Lines->Strings[ct++] = "Into a vector:" ;
  1248.     merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(),
  1249.             vResult.begin());
  1250.     for(vItr = vResult.begin(); vItr != vResult.end(); vItr++)
  1251.          Form1->Memo1->Lines->Strings[ct] =
  1252.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1253.     ct++;
  1254.  
  1255.         // merge into a list
  1256.     list<int> lResult;
  1257.     Form1->Memo1->Lines->Strings[ct++] = "Then a list:" ;
  1258.     merge (aVec.begin(), aVec.end(), aList.begin(), aList.end(),
  1259.             inserter(lResult, lResult.begin()));
  1260.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1261.          Form1->Memo1->Lines->Strings[ct] =
  1262.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1263.     ct++;
  1264.  
  1265. }
  1266.  
  1267. void set_example ()
  1268.     // illustrate the use of the generic set functions
  1269. {
  1270.     Form1->Memo1->Lines->Strings[ct++] = "Set operations:" ;
  1271.  
  1272.         // make a couple of ordered lists
  1273.     list <int> listOne, listTwo;
  1274.     list<int>::iterator lItr;
  1275.     generate_n (inserter(listOne, listOne.begin()), 5, iotaGen(1));
  1276.     generate_n (inserter(listTwo, listTwo.begin()), 5, iotaGen(3));
  1277.  
  1278.     Form1->Memo1->Lines->Strings[ct++] = "Original lists:" ;
  1279.     for(lItr = listOne.begin(); lItr != listOne.end(); lItr++)
  1280.          Form1->Memo1->Lines->Strings[ct] =
  1281.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1282.     ct++;
  1283.     for(lItr = listTwo.begin(); lItr != listTwo.end(); lItr++)
  1284.          Form1->Memo1->Lines->Strings[ct] =
  1285.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1286.     ct++;
  1287.         // now do the set operations
  1288.         // union - 1 2 3 4 5 6 7
  1289.     Form1->Memo1->Lines->Strings[ct++] = "Union:" ;
  1290.     list<int> lResult;
  1291.     set_union (listOne.begin(), listOne.end(),
  1292.         listTwo.begin(), listTwo.end(), inserter(lResult, lResult.begin()));
  1293.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1294.          Form1->Memo1->Lines->Strings[ct] =
  1295.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1296.     ct++;
  1297.     lResult.erase(lResult.begin(),lResult.end());
  1298.         // merge - 1 2 3 3 4 4 5 5 6 7
  1299.     Form1->Memo1->Lines->Strings[ct++] = "Merge:" ;
  1300.     merge (listOne.begin(), listOne.end(),
  1301.         listTwo.begin(), listTwo.end(), inserter(lResult, lResult.begin()));
  1302.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1303.          Form1->Memo1->Lines->Strings[ct] =
  1304.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1305.     ct++;
  1306.     lResult.erase(lResult.begin(),lResult.end());
  1307.         // intersection 3 4 5
  1308.     Form1->Memo1->Lines->Strings[ct++] = "Intersection:" ;
  1309.     set_intersection (listOne.begin(), listOne.end(),
  1310.         listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
  1311.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1312.          Form1->Memo1->Lines->Strings[ct] =
  1313.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1314.     ct++;
  1315.     lResult.erase(lResult.begin(),lResult.end());
  1316.         // difference 1 2
  1317.     Form1->Memo1->Lines->Strings[ct++] = "Difference:" ;
  1318.     set_difference (listOne.begin(), listOne.end(),
  1319.         listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
  1320.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1321.          Form1->Memo1->Lines->Strings[ct] =
  1322.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1323.     ct++;
  1324.     lResult.erase(lResult.begin(),lResult.end());
  1325.         // symmetric difference 1 2 6 7
  1326.     Form1->Memo1->Lines->Strings[ct++] = "Symmetric difference:" ;
  1327.     set_symmetric_difference (listOne.begin(), listOne.end(),
  1328.         listTwo.begin(), listTwo.end(),inserter(lResult, lResult.begin()));
  1329.     for(lItr = lResult.begin(); lItr != lResult.end(); lItr++)
  1330.          Form1->Memo1->Lines->Strings[ct] =
  1331.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1332.     ct++;
  1333.  
  1334.     if (includes(listOne.begin(), listOne.end(),
  1335.             listTwo.begin(), listTwo.end()))
  1336.                 Form1->Memo1->Lines->Strings[ct++] = "set is subset" ;
  1337.     else
  1338.         Form1->Memo1->Lines->Strings[ct++] = "set is not subset" ;
  1339.  
  1340. }
  1341.  
  1342.  
  1343. void heap_example ()
  1344.     // illustrate the use of the heap functions
  1345. {
  1346.     // make a heap of 15 random integers
  1347.     vector<int> aVec(15);
  1348.     vector<int>::iterator lItr;
  1349.     generate (aVec.begin(), aVec.end(), randomValue);
  1350.     make_heap (aVec.begin(), aVec.end());
  1351.     Form1->Memo1->Lines->Strings[ct++] = "Heap:" ;
  1352.     for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
  1353.          Form1->Memo1->Lines->Strings[ct] =
  1354.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1355.     ct++;
  1356.  
  1357.     Form1->Memo1->Lines->Strings[ct++] = "Largest value " + IntToStr(aVec.front() );
  1358.  
  1359.         // remove largest and reheap
  1360.     Form1->Memo1->Lines->Strings[ct++] = "Remove largest:" ;
  1361.     pop_heap(aVec.begin(), aVec.end());
  1362.     aVec.pop_back();
  1363.     for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
  1364.          Form1->Memo1->Lines->Strings[ct] =
  1365.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1366.     ct++;
  1367.  
  1368.         // add a 97 to the heap
  1369.     aVec.push_back(97);
  1370.     Form1->Memo1->Lines->Strings[ct++] = "Add 97:" ;
  1371.     push_heap (aVec.begin(), aVec.end());
  1372.     for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
  1373.          Form1->Memo1->Lines->Strings[ct] =
  1374.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1375.     ct++;
  1376.  
  1377.         // finally, make into sorted collection
  1378.     Form1->Memo1->Lines->Strings[ct++] = "Sort:" ;
  1379.     sort_heap (aVec.begin(), aVec.end());
  1380.     for(lItr = aVec.begin(); lItr != aVec.end(); lItr++)
  1381.          Form1->Memo1->Lines->Strings[ct] =
  1382.           Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  1383.     ct++;
  1384. }
  1385.  
  1386. int alg7_ex() { /* alg7 */
  1387.     Form1->Memo1->Lines->Strings[ct++] = "===== STL generic algorithms - Sorting  ===";
  1388.  
  1389.     sortExample();
  1390.     partial_sort_example();
  1391.     nth_element_example();
  1392.     binary_search_example();
  1393.     merge_example();
  1394.     set_example();
  1395.     heap_example();
  1396.  
  1397.     Form1->Memo1->Lines->Strings[ct++] = "End sorting examples" ;
  1398.     return 0;
  1399. }
  1400.  
  1401.  
  1402. // *** start auto_ptr source
  1403.  
  1404. //
  1405. // A simple structure.
  1406. //
  1407.  
  1408. struct X
  1409. {
  1410.     X (int i = 0) : m_i(i) { }
  1411.     int get() const { return m_i; }
  1412.     int m_i;
  1413. };
  1414.  
  1415. int auto_ptr () /* auto_ptr */
  1416. {
  1417.  
  1418.    Form1->Memo1->Lines->Strings[ct++] = " =========== Auto pointer Example ========";
  1419.     //
  1420.     // b will hold a pointer to an X.
  1421.     //
  1422.     std::auto_ptr<X> b(new X(12345));
  1423.     //
  1424.     // a will now be the owner of the underlying pointer.
  1425.     //
  1426.     std::auto_ptr<X> a = b;
  1427.     //
  1428.     // Output the value contained by the underlying pointer.
  1429.     //
  1430.     Form1->Memo1->Lines->Strings[ct++] = a->get() ;
  1431.     //
  1432.     // The pointer will be delete'd when a is destroyed on leaving scope.
  1433.     //
  1434.     return 0;
  1435. }
  1436.  
  1437.  
  1438.  int binders () /* binders */
  1439.  {
  1440.    typedef vector<int>::iterator iterator;
  1441.    int d1[4] = {1,2,3,4};
  1442.    Form1->Memo1->Lines->Strings[ct++] = " ============= Binders Example ===========";
  1443.    //
  1444.    // Set up a vector.
  1445.    //
  1446.    vector<int> v1(d1+0, d1+4);
  1447.    //
  1448.    // Create an 'equal to 3' unary predicate by binding 3 to
  1449.    // the equal_to binary predicate.
  1450.    //
  1451.    binder1st<equal_to<int> > equal_to_3 = bind1st(equal_to<int>(),3);
  1452.    //
  1453.    // Now use this new predicate in a call to find_if.
  1454.    //
  1455.    iterator it1 = find_if(v1.begin(),v1.end(),equal_to_3);
  1456.    //
  1457.    // Even better, construct the new predicate on the fly.
  1458.    //
  1459.    iterator it2 = find_if(v1.begin(),v1.end(),bind1st(equal_to<int>(),3));
  1460.    //
  1461.    // And now the same thing using bind2nd.
  1462.    // Same result since == is commutative.
  1463.    //
  1464.    iterator it3 = find_if(v1.begin(),v1.end(),bind2nd(equal_to<int>(),3));
  1465.    //
  1466.    // Output results.
  1467.    //
  1468.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(*it1) + " " + IntToStr(*it2) + " " + IntToStr(*it3) ;
  1469.  
  1470.    return 0;
  1471.  }
  1472.  
  1473.  
  1474. int bitset () /* bitset */
  1475. {
  1476.     Form1->Memo1->Lines->Strings[ct++] = " ============= Bitset Example ============";
  1477.     std::bitset<8> b;
  1478.     Form1->Memo1->Lines->Strings[ct++] = "Initial value:        " + (String)(b.to_string()).data() ;
  1479.     b |= 5;
  1480.     Form1->Memo1->Lines->Strings[ct++] = "After |= 5 operation: " +(String)(b.to_string()).data() ; // results in 00000101
  1481.     return 0;
  1482. }
  1483.  
  1484.  
  1485. int complex1 () /* complex */
  1486. {
  1487.  
  1488.    Form1->Memo1->Lines->Strings[ct++] = " ============= Complex Example ===========";
  1489.  
  1490.    complex<double> a(1.2, 3.4);
  1491.    complex<double> b(-9.8, -7.6);
  1492.  
  1493.    a += b;
  1494.    a /= sin(b) * cos(a);
  1495.    b *= log(a) + pow(b, a);
  1496.  
  1497.    Form1->Memo1->Lines->Strings[ct++] = "a = (" + FloatToStr((long double)a.real()) +
  1498.       "," + FloatToStr((long double)a.imag()) + ")";
  1499.    Form1->Memo1->Lines->Strings[ct++] = "b = (" + FloatToStr((long double)b.real()) +
  1500.       "," + FloatToStr((long double)b.imag()) + ")" ;
  1501.  
  1502.    return 0;
  1503.  }
  1504.  
  1505. // *** start complx source
  1506.  
  1507. typedef complex<double> dcomplex;
  1508.  
  1509. pair<dcomplex, dcomplex> quadratic
  1510.     (dcomplex a, dcomplex b, dcomplex c)
  1511.         // return roots of a quadratic equation
  1512. {
  1513.     dcomplex root = sqrt(b * b - 4.0 * a * c);
  1514.     a = a * 2.0;
  1515.  
  1516.     return make_pair (
  1517.         (-b + root) / a,
  1518.         (-b - root) / a);
  1519. }
  1520.  
  1521. int complex2() { /* complx */
  1522.     dcomplex a(2, 3);
  1523.     dcomplex b(4, 5);
  1524.     dcomplex c(6, 7);
  1525.  
  1526.    Form1->Memo1->Lines->Strings[ct++] = " ============ Complex2 Example ===========";
  1527.     pair<dcomplex, dcomplex> ans = quadratic(a, b, c);
  1528.     Form1->Memo1->Lines->Strings[ct++] = "Roots are: (" + FloatToStr((long double)ans.first.real()) +
  1529.        "," + FloatToStr((long double)ans.first.imag()) + ")";
  1530.     Form1->Memo1->Lines->Strings[ct++] = "           (" + FloatToStr((long double)ans.second.real()) +
  1531.        "," + FloatToStr((long double)ans.second.imag()) + ")" ;
  1532.     return 0;
  1533. }
  1534.  
  1535.  
  1536. int copy_ex () /* copyex */
  1537. {
  1538.    int d1[4] = {1,2,3,4};
  1539.    int d2[4] = {5,6,7,8};
  1540.    Form1->Memo1->Lines->Strings[ct++] = " ============== CopyEx Example ===========";
  1541.    //
  1542.    // Set up three vectors.
  1543.    //
  1544.    vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4), v3(d2+0, d2+4);
  1545.    //
  1546.    // Set up one empty vector.
  1547.    //
  1548.    vector<int> v4;
  1549.    //
  1550.    // Copy v1 to v2.
  1551.    //
  1552.    copy(v1.begin(), v1.end(), v2.begin());
  1553.    //
  1554.    // Copy backwards v1 to v3.
  1555.    //
  1556.    copy_backward(v1.begin(), v1.end(), v3.end());
  1557.    //
  1558.    // Use insert iterator to copy into empty vector.
  1559.    //
  1560.    copy(v1.begin(), v1.end(), back_inserter(v4));
  1561.    //
  1562.    // Copy all four to cout.
  1563.    //
  1564.    vector<int>::iterator out;
  1565.    for(out = v1.begin(); out != v1.end(); out++)
  1566.      Form1->Memo1->Lines->Strings[ct] =
  1567.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*out) + " ";
  1568.    ct++;
  1569.  
  1570.    return 0;
  1571.  }
  1572.  
  1573.  int count_ex () /*count */
  1574.  {
  1575.    int sequence[10] = {1,2,3,4,5,5,7,8,9,10};
  1576.    int i=0,j=0,k=0;
  1577.    Form1->Memo1->Lines->Strings[ct++] = " ============== Count Example ============";
  1578.    //
  1579.    // Set up a vector.
  1580.    //
  1581.    vector<int> v(sequence+0, sequence+10);
  1582.  
  1583.    count(v.begin(),v.end(),5,i);    // Count fives
  1584.    count(v.begin(),v.end(),6,j);    // Count sixes
  1585.    //
  1586.    // Count all less than 8.
  1587.    //
  1588.    count_if(v.begin(), v.end(), bind2nd(less<int>(),8),k);
  1589.  
  1590.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(i) + " " + IntToStr(j) + " " + IntToStr(k) ;
  1591.  
  1592.    return 0;
  1593.  }
  1594.  
  1595. // *** start deque source
  1596.  
  1597.  deque<string> deck_of_cards;
  1598.  deque<string> current_hand;
  1599.  
  1600.  void initialize_cards(deque<string>& cards)
  1601.  {
  1602.    cards.push_front("aceofspades");
  1603.    cards.push_front("kingofspades");
  1604.    cards.push_front("queenofspades");
  1605.    cards.push_front("jackofspades");
  1606.    cards.push_front("tenofspades");
  1607.    //
  1608.    // etc.
  1609.    //
  1610.  }
  1611.  
  1612.  template <class It, class It2>
  1613.  void print_current_hand(It start, It2 end)
  1614.  {
  1615.    while (start < end)
  1616.      Form1->Memo1->Lines->Strings[ct++] = (*start++).data() ;
  1617.  }
  1618.  
  1619.  
  1620.  template <class It, class It2>
  1621.  void deal_cards(It, It2 end)
  1622.  {
  1623.    for (int i=0;i<5;i++)
  1624.    {
  1625.      current_hand.insert(current_hand.begin(),*end);
  1626.      deck_of_cards.erase(end++);
  1627.    }
  1628.  }
  1629.  
  1630.  void play_poker()
  1631.  {
  1632.    initialize_cards(deck_of_cards);
  1633.    deal_cards(current_hand.begin(),deck_of_cards.begin());
  1634.  }
  1635.  
  1636.  int deque_ex () /* deque */
  1637.  {
  1638.    Form1->Memo1->Lines->Strings[ct++] = " ============= Deque Example =============";
  1639.    play_poker();
  1640.    print_current_hand(current_hand.begin(),current_hand.end());
  1641.    return 0;
  1642.  }
  1643.  
  1644.  
  1645.  int distance_ex() /* distance */
  1646.  {
  1647.    Form1->Memo1->Lines->Strings[ct++] = " ============= Distance Example ==========";
  1648.    //
  1649.    // Initialize a vector using an array.
  1650.    //
  1651.    int arr[6] = {3,4,5,6,7,8};
  1652.    vector<int> v(arr+0, arr+6);
  1653.    //
  1654.    // Declare a list iterator, s.b. a ForwardIterator.
  1655.    //
  1656.    vector<int>::iterator itr;
  1657.    //
  1658.    // Output the original vector.
  1659.    //
  1660.    Form1->Memo1->Lines->Strings[ct++] = "For the vector: ";
  1661.    for(itr = v.begin(); itr != v.end(); itr++)
  1662.      Form1->Memo1->Lines->Strings[ct] =
  1663.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1664.    ct++;
  1665.    itr = v.begin()+3;
  1666.    Form1->Memo1->Lines->Strings[ct++] = "When the iterator is initialized to point to " + IntToStr(*itr) ;
  1667.    //
  1668.    // Use of distance.
  1669.    //
  1670.    vector<int>::difference_type dist;
  1671.    distance(v.begin(), itr, dist);
  1672.    Form1->Memo1->Lines->Strings[ct++] = "The distance between the beginning and itr is " + IntToStr(dist) ;
  1673.  
  1674.    return 0;
  1675.  }
  1676.  
  1677.  
  1678.  int eqlrange_ex () /* eqlrange */
  1679.  {
  1680.    typedef vector<int>::iterator iterator;
  1681.    Form1->Memo1->Lines->Strings[ct++] = " =========== Equal Range Example =========";
  1682.    int d1[11] = {0,1,2,2,3,4,2,2,2,6,7};
  1683.    //
  1684.    // Set up a vector.
  1685.    //
  1686.    vector<int> v1(d1+0, d1+11);
  1687.    //
  1688.    // Try equal_range variants.
  1689.    //
  1690.    pair<iterator,iterator> p1 = equal_range(v1.begin(),v1.end(),3);
  1691.    pair<iterator,iterator> p2 = equal_range(v1.begin(),v1.end(),2,less<int>());
  1692.    //
  1693.    // Output results.
  1694.    //
  1695.    Form1->Memo1->Lines->Strings[ct++] =  "The equal range for 3 is: ";
  1696.    Form1->Memo1->Lines->Strings[ct++] = "( " + IntToStr(*p1.first)+" " + IntToStr(*p1.second) + " ) "  ;
  1697.  
  1698.    Form1->Memo1->Lines->Strings[ct++] =  "The equal range for 2 is: ";
  1699.    Form1->Memo1->Lines->Strings[ct++] =  "( " + IntToStr(*p2.first) + "  "
  1700.         + IntToStr(*p2.second) + " ) " ;
  1701.  
  1702.    return 0;
  1703.  }
  1704.  
  1705.  
  1706.  int equal_ex () /* equal */
  1707.  {
  1708.    int d1[4] = {1,2,3,4};
  1709.    int d2[4] = {1,2,4,3};
  1710.    Form1->Memo1->Lines->Strings[ct++] = " ============== Equal Example ===========";
  1711.    //
  1712.    // Set up two vectors.
  1713.    //
  1714.    vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4);
  1715.    //
  1716.    // Check for equality.
  1717.    //
  1718.    bool b1 = equal(v1.begin(), v1.end(), v2.begin());
  1719.    bool b2 = equal(v1.begin(), v1.end(), v2.begin(),equal_to<int>());
  1720.    //
  1721.    // Both b1 and b2 are false.
  1722.    //
  1723.    Form1->Memo1->Lines->Strings[ct++] = (String)(b1 ? "TRUE" : "FALSE")  + " "
  1724.         + (String)(b2 ? "TRUE" : "FALSE") ;
  1725.    return 0;
  1726.  }
  1727.  
  1728.  
  1729.  int fill_ex () /* fill */
  1730.  {
  1731.    int d1[4] = {1,2,3,4};
  1732.    //
  1733.    // Set up two vectors.
  1734.    //
  1735.    Form1->Memo1->Lines->Strings[ct++] = " =============== Fill Example ============";
  1736.    vector<int> v1(d1+0, d1+4), v2(d1+0, d1+4);
  1737.    //
  1738.    // Set up one empty vector.
  1739.    //
  1740.    vector<int> v3;
  1741.    vector<int>::iterator itr;
  1742.    //
  1743.    // Fill all of v1 with 9.
  1744.    //
  1745.    fill(v1.begin(), v1.end(), 9);
  1746.    //
  1747.    // Fill first 3 of v2 with 7.
  1748.    //
  1749.    fill_n(v2.begin(), 3, 7);
  1750.    //
  1751.    // Use insert iterator to fill v3 with 5 11's.
  1752.    //
  1753.    fill_n(back_inserter(v3), 5, 11);
  1754.    //
  1755.    // Copy all three to cout.
  1756.    //
  1757.  
  1758.    for(itr = v1.begin(); itr != v1.end(); itr++)
  1759.      Form1->Memo1->Lines->Strings[ct] =
  1760.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1761.    ct++;
  1762.    for(itr = v2.begin(); itr != v2.end(); itr++)
  1763.      Form1->Memo1->Lines->Strings[ct] =
  1764.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1765.    ct++;
  1766.    for(itr = v3.begin(); itr != v3.end(); itr++)
  1767.      Form1->Memo1->Lines->Strings[ct] =
  1768.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1769.    ct++;
  1770.  
  1771.  
  1772.    //
  1773.    // Fill cout with 3 5's.
  1774.    //
  1775.    fill_n(v1.begin(), 3, 5);
  1776.    for(itr = v1.begin(); itr != v1.end(); itr++)
  1777.      Form1->Memo1->Lines->Strings[ct] =
  1778.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1779.    ct++;
  1780.  
  1781.    return 0;
  1782.  }
  1783.  
  1784.  
  1785.  int find_ex () /* find */
  1786.  {
  1787.    typedef vector<int>::iterator iterator;
  1788.    Form1->Memo1->Lines->Strings[ct++] = " =============== Find Example ============";
  1789.    int d1[10] = {0,1,2,2,3,4,2,2,6,7};
  1790.    //
  1791.    // Set up a vector.
  1792.    //
  1793.    vector<int> v1(d1+0, d1+10);
  1794.    //
  1795.    // Try find.
  1796.    //
  1797.    iterator it1 = find(v1.begin(), v1.end(), 3);
  1798.    //
  1799.    // Try find_if.
  1800.    //
  1801.    iterator it2 = find_if(v1.begin(), v1.end(), bind1st(equal_to<int>(), 3));
  1802.    //
  1803.    // Try both adjacent_find variants.
  1804.    //
  1805.    iterator it3 = adjacent_find(v1.begin(), v1.end());
  1806.    iterator it4 = adjacent_find(v1.begin(), v1.end(), equal_to<int>());
  1807.    //
  1808.    // Output results.
  1809.    //
  1810.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(*it1) + " " + IntToStr(*it2) + " " + IntToStr(*it3) + " " + IntToStr(*it4) ;
  1811.  
  1812.    return 0;
  1813.  }
  1814.  
  1815.  
  1816.  int find_fo_ex() /* find_f_o */
  1817.  {
  1818.    typedef vector<int>::iterator iterator;
  1819.    int d1[10] = {0,1,2,2,3,4,2,2,6,7};
  1820.    int d2[2] = {6,4};
  1821.    Form1->Memo1->Lines->Strings[ct++] = " ========== Find First Of Example ========";
  1822.    //
  1823.    // Set up two vectors.
  1824.    //
  1825.    vector<int> v1(d1+0, d1+10), v2(d2+0, d2+2);
  1826.    vector<int>::iterator vItr;
  1827.    //
  1828.    // Try both find_first_of variants.
  1829.    //
  1830.    iterator it1 = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
  1831.  
  1832.    find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end(), equal_to<int>());
  1833.    //
  1834.    // Output results.
  1835.    //
  1836.    Form1->Memo1->Lines->Strings[ct++] = "For the vectors: ";
  1837.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  1838.      Form1->Memo1->Lines->Strings[ct] =
  1839.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1840.    ct++;
  1841.    Form1->Memo1->Lines->Strings[ct++] = " and ";
  1842.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  1843.      Form1->Memo1->Lines->Strings[ct] =
  1844.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1845.    ct++;
  1846.    Form1->Memo1->Lines->Strings[ct++] = "both versions of find_first_of point to: "+IntToStr(*it1);
  1847.  
  1848.    return 0;
  1849.  }
  1850.  
  1851. // *** start for_each source
  1852.  
  1853.  //
  1854.  // Function class that outputs its argument times x.
  1855.  //
  1856.  template <class Arg>
  1857.  class out_times_x :  private unary_function<Arg,void>
  1858.  {
  1859.    private:
  1860.       Arg multiplier;
  1861.    public:
  1862.       out_times_x(const Arg& x) : multiplier(x) { }
  1863.       void operator()(const Arg& x) {Form1->Memo1->Lines->Strings[ct] =
  1864.        Form1->Memo1->Lines->Strings[ct] + IntToStr (x * multiplier) + " " ; }
  1865.  };
  1866.  
  1867.  int for_each_ex () /* for_each */
  1868.  {
  1869.    int sequence[5] = {1,2,3,4,5};
  1870.    Form1->Memo1->Lines->Strings[ct++] = " ============= For Each Example =========";
  1871.    //
  1872.    // Set up a vector.
  1873.    //
  1874.    vector<int> v(sequence+0, sequence+5);
  1875.    vector<int>::iterator itr;
  1876.    Form1->Memo1->Lines->Strings[ct++] = " Original vector:";
  1877.    for(itr = v.begin(); itr != v.end(); itr++)
  1878.      Form1->Memo1->Lines->Strings[ct] =
  1879.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1880.    ct++;
  1881.    //
  1882.    // Setup a function object.
  1883.    //
  1884.    out_times_x<int> f2(2);
  1885.    Form1->Memo1->Lines->Strings[ct++] = " After times-two operation:";
  1886.  
  1887.    for_each(v.begin(),v.end(),f2);   // Apply function
  1888.    ct++;
  1889.  
  1890.    return 0;
  1891.  }
  1892.  
  1893.  
  1894.  //
  1895.  // Create a new function object from unary_function.
  1896.  //
  1897.  template<class Arg>
  1898.  class factorial : public unary_function<Arg, Arg>
  1899.  {
  1900.    public:
  1901.      Arg operator() (const Arg& arg);
  1902.  };
  1903.  
  1904.  template<class Arg>
  1905.  Arg factorial<Arg>::operator() (const Arg& arg)
  1906.  {
  1907.     Arg a = 1;
  1908.     for (Arg i = 2; i <= arg; i++)
  1909.     a *= i;
  1910.     return a;
  1911.  }
  1912.  
  1913.  int funct_ob_ex () /* funct_ob*/
  1914.  {
  1915.    Form1->Memo1->Lines->Strings[ct++] = " ============= Funct_ob Example =========";
  1916.    //
  1917.    // Initialize a deque with an array of integers.
  1918.    //
  1919.    int init[7] = {1,2,3,4,5,6,7};
  1920.    deque<int> d(init+0, init+7);
  1921.    deque<int>::iterator dItr;
  1922.    //
  1923.    // Create an empty vector to store the factorials.
  1924.    //
  1925.    vector<int> v((size_t)7);
  1926.    vector<int>::iterator itr;
  1927.    //
  1928.    // Transform the numbers in the deque to their factorials and store
  1929.    // in the vector.
  1930.    //
  1931.    transform(d.begin(), d.end(), v.begin(), factorial<int>());
  1932.    //
  1933.    // Print the results.
  1934.    //
  1935.    Form1->Memo1->Lines->Strings[ct++] = "The following numbers: " ;
  1936.    for(dItr = d.begin(); dItr != d.end(); dItr++)
  1937.      Form1->Memo1->Lines->Strings[ct] =
  1938.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*dItr) + " ";
  1939.    ct++;
  1940.  
  1941.    Form1->Memo1->Lines->Strings[ct++] = "Have the factorials: "  ;
  1942.    for(itr = v.begin(); itr != v.end(); itr++)
  1943.      Form1->Memo1->Lines->Strings[ct] =
  1944.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*itr) + " ";
  1945.    ct++;
  1946.  
  1947.    return 0;
  1948.  }
  1949.  
  1950.  
  1951.  
  1952.  
  1953.  int heap_opts_ex () /* heap_opts */
  1954.  {
  1955.    int d1[4] = {1,2,3,4};
  1956.    int d2[4] = {1,3,2,4};
  1957.    Form1->Memo1->Lines->Strings[ct++] = " ============== Heap Example ===========";
  1958.    //
  1959.    // Set up two vectors.
  1960.    //
  1961.    vector<int> v1(d1+0, d1+4), v2(d2+0, d2+4);
  1962.    vector<int> vResult;
  1963.    vector<int>::iterator vItr;
  1964.    //
  1965.    // Make heaps.
  1966.    //
  1967.    make_heap(v1.begin(), v1.end());
  1968.    make_heap(v2.begin(), v2.end(), less<int>());
  1969.    //
  1970.    // v1 = (4,x,y,z)  and  v2 = (4,x,y,z)
  1971.    //
  1972.    // Note that x, y and z represent the remaining values in the
  1973.    // container (other than 4).  The definition of the heap and heap
  1974.    // operations  does not require any particular ordering
  1975.    // of these values.
  1976.    //
  1977.    // Copy both vectors to cout.
  1978.    //
  1979.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  1980.      Form1->Memo1->Lines->Strings[ct] =
  1981.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1982.    ct++;
  1983.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  1984.      Form1->Memo1->Lines->Strings[ct] =
  1985.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1986.    ct++;
  1987.    //
  1988.    // Now let's pop.
  1989.    //
  1990.    pop_heap(v1.begin(), v1.end());
  1991.    pop_heap(v2.begin(), v2.end(), less<int>());
  1992.    //
  1993.    // Copy both vectors to cout.
  1994.    //
  1995.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  1996.      Form1->Memo1->Lines->Strings[ct] =
  1997.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  1998.    ct++;
  1999.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  2000.      Form1->Memo1->Lines->Strings[ct] =
  2001.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2002.    ct++;
  2003.    //
  2004.    // And push.
  2005.    //
  2006.    push_heap(v1.begin(), v1.end());
  2007.    push_heap(v2.begin(), v2.end(), less<int>());
  2008.    //
  2009.    // Copy both vectors to cout.
  2010.    //
  2011.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  2012.      Form1->Memo1->Lines->Strings[ct] =
  2013.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2014.    ct++;
  2015.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  2016.      Form1->Memo1->Lines->Strings[ct] =
  2017.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2018.    ct++;
  2019.    //
  2020.    // Now sort those heaps.
  2021.    //
  2022.    sort_heap(v1.begin(), v1.end());
  2023.    sort_heap(v2.begin(), v2.end(), less<int>());
  2024.    //
  2025.    // Copy both vectors to cout.
  2026.    //
  2027.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  2028.      Form1->Memo1->Lines->Strings[ct] =
  2029.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2030.    ct++;
  2031.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  2032.      Form1->Memo1->Lines->Strings[ct] =
  2033.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2034.    ct++;
  2035.  
  2036.    return 0;
  2037.  }
  2038.  
  2039. //  Begin ice cream simulation example source
  2040.  
  2041. //    class event
  2042. //    execution event in a descrete event driven simulation
  2043. //
  2044.  
  2045. class event {
  2046. public:
  2047.     // construct sets time of event
  2048.     event (unsigned int t) : time(t) { };
  2049.  
  2050.     // time is a public data field
  2051.     const unsigned int time;
  2052.  
  2053.     // execute event my invoking this method
  2054.     virtual void processEvent() = 0;
  2055. };
  2056.  
  2057. inline void destroy(event **) {}
  2058.  
  2059. struct eventComparison {
  2060.     bool operator () (event * left, event * right)
  2061.         { return left->time > right->time; }
  2062. };
  2063.  
  2064. //
  2065. //    class simulation
  2066. //        framework for discrete event-driven simulations
  2067. //    written by Tim Budd, for Rogue Wave, 1995
  2068. //
  2069.  
  2070. class simulation {
  2071. public:
  2072.         // constructor
  2073.     simulation () : eventQueue(), time(0) {}
  2074.  
  2075.         // start and run simulation
  2076.     void run ();
  2077.     
  2078.         // return current time
  2079.     unsigned int time;
  2080.     
  2081.         // schedule a future event
  2082.     void  scheduleEvent (event * newEvent)
  2083.         { eventQueue.push(newEvent); }
  2084.     
  2085. protected:
  2086.     priority_queue<event*, vector<event *>, eventComparison> eventQueue;
  2087. };
  2088.  
  2089. void simulation::run()
  2090. {
  2091.     while (! eventQueue.empty()) {
  2092.         event * nextEvent = eventQueue.top();
  2093.         eventQueue.pop();
  2094.         time = nextEvent->time;
  2095.         nextEvent->processEvent();
  2096.         delete nextEvent;
  2097.         }
  2098. }
  2099.  
  2100. //
  2101. //    ice cream store simulation
  2102. //
  2103.  
  2104. class storeSimulation : public simulation {
  2105. public:
  2106.     storeSimulation()
  2107.         : freeChairs(35), profit(0.0), simulation() { }
  2108.         
  2109.     bool canSeat(unsigned int numberOfPeople);
  2110.     void order(unsigned int numberOfScoops);
  2111.     void leave(unsigned int numberOfPeople);
  2112.  
  2113.         // data fields
  2114.     unsigned int freeChairs;
  2115.     double profit;
  2116. } theSimulation;
  2117.  
  2118. class arriveEvent : public event {
  2119. public:
  2120.     arriveEvent (unsigned int time, unsigned int groupSize)
  2121.         : event(time), size(groupSize) { }
  2122.     virtual void processEvent();
  2123. private:
  2124.     unsigned int size;
  2125. };
  2126.  
  2127. class orderEvent : public event {
  2128. public:
  2129.     orderEvent (unsigned int time, unsigned int groupSize)
  2130.         : event(time), size(groupSize) { }
  2131.     virtual void processEvent();
  2132. private:
  2133.     unsigned int size;
  2134. };
  2135.  
  2136. class leaveEvent : public event {
  2137. public:
  2138.     leaveEvent (unsigned int time, unsigned int groupSize)
  2139.         : event(time), size(groupSize) { }
  2140.     virtual void processEvent();
  2141. private:
  2142.     unsigned int size;
  2143. };
  2144.  
  2145. int irand(int n)
  2146.     // return random integer between 0 and n
  2147. { return (rand()/10) % n; }
  2148.  
  2149. void arriveEvent::processEvent()
  2150. {
  2151.     if (theSimulation.canSeat(size))
  2152.         theSimulation.scheduleEvent(new orderEvent(time + 1 + irand(4), size));
  2153. }
  2154.  
  2155. void orderEvent::processEvent()
  2156. {
  2157.         // each person orders some number of scoops
  2158.     for (int i = 0; i < size; i++)
  2159.         theSimulation.order(1 + irand(4));
  2160.         // then we schedule the leave event
  2161.     theSimulation.scheduleEvent(new leaveEvent(time + 1 + irand(10), size));
  2162. }
  2163.  
  2164. void leaveEvent::processEvent()
  2165. {
  2166.     theSimulation.leave(size);
  2167. }
  2168.  
  2169. bool storeSimulation::canSeat(unsigned int numberOfPeople)
  2170.     // if sufficient room then seat customers
  2171. {
  2172.     Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
  2173.         " group of " + IntToStr(numberOfPeople) + " customers arrives";
  2174.     if (numberOfPeople < freeChairs) {
  2175.         Form1->Memo1->Lines->Strings[ct++] = " is seated" ;
  2176.         freeChairs -= numberOfPeople;
  2177.         return true;
  2178.         }
  2179.     else {
  2180.         Form1->Memo1->Lines->Strings[ct++] = " no room, they leave";
  2181.         return false;
  2182.         }
  2183. }
  2184.  
  2185. void storeSimulation::order(unsigned int numberOfScoops)
  2186.     // service icecream, compute profits
  2187. {
  2188.     Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
  2189.          " serviced order for " + IntToStr(numberOfScoops) ;
  2190.     profit += 0.35 * numberOfScoops;
  2191. }
  2192.  
  2193. void storeSimulation::leave(unsigned int numberOfPeople)
  2194.     // people leave, free up chairs
  2195. {
  2196.     Form1->Memo1->Lines->Strings[ct++] = "Time: " + IntToStr(time) +
  2197.         " group of size " + IntToStr(numberOfPeople) + " leaves" ;
  2198.     freeChairs += numberOfPeople;
  2199. }
  2200.  
  2201. int ice_cream_ex() {
  2202.     Form1->Memo1->Lines->Strings[ct++] = "============ Ice Cream Store simulation from Chapter 9";
  2203.         // load queue with some number of initial events
  2204.     unsigned int t = 0;
  2205.     while (t < 20) {
  2206.         t += irand(6);
  2207.         Form1->Memo1->Lines->Strings[ct++] = "pumping queue with event " + IntToStr(t);
  2208.         theSimulation.scheduleEvent(new arriveEvent(t, 1 + irand(4)));
  2209.         }
  2210.  
  2211.         // run the simulation
  2212.     theSimulation.run();
  2213.     Form1->Memo1->Lines->Strings[ct++] = "Total profits " + FloatToStr(theSimulation.profit);
  2214.  
  2215.     Form1->Memo1->Lines->Strings[ct++] = "End of ice cream store simulation" ;
  2216.     return 0;
  2217. }
  2218.  
  2219.  
  2220. int innerprod_ex ()
  2221.  {
  2222.    //
  2223.    // Initialize a list and an int using arrays of ints.
  2224.    //
  2225.    int a1[3] = {6, -3, -2};
  2226.    int a2[3] = {-2, -3, -2};
  2227.  
  2228.    list<int>   l(a1+0, a1+3);
  2229.    vector<int> v(a2+0, a2+3);
  2230.    list<int>::iterator lItr;
  2231.    vector<int>::iterator vItr;
  2232.    Form1->Memo1->Lines->Strings[ct++] = " =========== Inner Product Example ========";
  2233.    //
  2234.    // Calculate the inner product of the two sets of values.
  2235.    //
  2236.    int inner_prod = inner_product(l.begin(), l.end(), v.begin(), 0);
  2237.    //
  2238.    // Calculate a wacky inner product using the same values.
  2239.    //
  2240.    int wacky = inner_product(l.begin(), l.end(), v.begin(), 0,
  2241.                              plus<int>(), minus<int>());
  2242.    //
  2243.    // Print the output.
  2244.    //
  2245.    Form1->Memo1->Lines->Strings[ct++] = "For the two sets of numbers: " ;
  2246.    for(vItr = v.begin(); vItr != v.end(); vItr++)
  2247.      Form1->Memo1->Lines->Strings[ct] =
  2248.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2249.    ct++;
  2250.    Form1->Memo1->Lines->Strings[ct++] = " and  ";
  2251.    for(lItr = l.begin(); lItr != l.end(); lItr++)
  2252.      Form1->Memo1->Lines->Strings[ct] =
  2253.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*lItr) + " ";
  2254.    ct++;
  2255.  
  2256.    Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct]+ "," ;
  2257.    Form1->Memo1->Lines->Strings[ct++] = "The inner product is: " + IntToStr(inner_prod) ;
  2258.    Form1->Memo1->Lines->Strings[ct++] = "The wacky result is: " + IntToStr(wacky);
  2259.  
  2260.    return 0;
  2261.  }
  2262.  
  2263.  
  2264.  int lex_comp_ex(void)
  2265.  {
  2266.    Form1->Memo1->Lines->Strings[ct++] = " ======== Lexicographic compare Example ========";
  2267.    int d1[5] = {1,3,5,32,64};
  2268.    int d2[5] = {1,3,2,43,56};
  2269.    //
  2270.    // Set up vector.
  2271.    //
  2272.    vector<int> v1(d1+0, d1+5), v2(d2+0, d2+5);
  2273.    //
  2274.    // Is v1 less than v2 (I think not).
  2275.    //
  2276.    bool b1 = lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end());
  2277.    //
  2278.    // Is v2 less than v1 (yup, sure is).
  2279.    //
  2280.    bool b2 = lexicographical_compare(v2.begin(), v2.end(),
  2281.                                      v1.begin(), v1.end(), less<int>());
  2282.    Form1->Memo1->Lines->Strings[ct++] = (String)(b1 ? "TRUE" : "FALSE") + " "
  2283.         + (String)(b2 ? "TRUE" : "FALSE") ;
  2284.  
  2285.    return 0;
  2286.  }
  2287.  
  2288.  int list_ex ()
  2289.  {
  2290.    //
  2291.    // Create a list of critters.
  2292.    //
  2293.    list<string> critters;
  2294.    list<string>::iterator lItr;
  2295.    int i;
  2296.    Form1->Memo1->Lines->Strings[ct++] = " ========== List Example ========";
  2297.    //
  2298.    // Insert several critters.
  2299.    //
  2300.    critters.insert(critters.begin(),"antelope");
  2301.    critters.insert(critters.begin(),"bear");
  2302.    critters.insert(critters.begin(),"cat");
  2303.    //
  2304.    // Print out the list.
  2305.    //
  2306.    for(lItr = critters.begin(); lItr != critters.end(); lItr++)
  2307.      Form1->Memo1->Lines->Strings[ct] =
  2308.        Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
  2309.    ct++;
  2310.    //
  2311.    // Change cat to cougar.
  2312.    //
  2313.    *find(critters.begin(),critters.end(),"cat") = "cougar";
  2314.    for(lItr = critters.begin(); lItr != critters.end(); lItr++)
  2315.      Form1->Memo1->Lines->Strings[ct] =
  2316.        Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
  2317.    ct++;
  2318.    //
  2319.    // Put a zebra at the beginning, an ocelot ahead of antelope,
  2320.    // and a rat at the end.
  2321.    //
  2322.    critters.push_front("zebra");
  2323.    critters.insert(find(critters.begin(),critters.end(),"antelope"),"ocelot");
  2324.    critters.push_back("rat");
  2325.    for(lItr = critters.begin(); lItr != critters.end(); lItr++)
  2326.      Form1->Memo1->Lines->Strings[ct] =
  2327.        Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
  2328.    ct++;
  2329.    //
  2330.    // Sort the list (Use list's sort function since the
  2331.    // generic algorithm requires a random access iterator
  2332.    // and list only provides bidirectional)
  2333.    //
  2334.    critters.sort();
  2335.    for(lItr = critters.begin(); lItr != critters.end(); lItr++)
  2336.      Form1->Memo1->Lines->Strings[ct] =
  2337.        Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
  2338.    ct++;
  2339.    //
  2340.    // Now let's erase half of the critters.
  2341.    //
  2342.    int half = critters.size() / 2;
  2343.    for (i = 0; i < half; ++i)
  2344.      critters.erase(critters.begin());
  2345.    for(lItr = critters.begin(); lItr != critters.end(); lItr++)
  2346.      Form1->Memo1->Lines->Strings[ct] =
  2347.        Form1->Memo1->Lines->Strings[ct] + (String)(*lItr).data() + " ";
  2348.    ct++;
  2349.  
  2350.    return 0;
  2351.  }
  2352.  
  2353.  
  2354.  
  2355.  int merge_ex ()
  2356.  {
  2357.    int d1[4] = {1,2,3,4};
  2358.    int d2[8] = {11,13,15,17,12,14,16,18};
  2359.    //
  2360.    // Set up two vectors.
  2361.    //
  2362.    Form1->Memo1->Lines->Strings[ct++] = " ============ Merge Example ========";
  2363.    vector<int> v1(d1+0, d1+4), v2(d1+0, d1+4);
  2364.    //
  2365.    // Set up four destination vectors.
  2366.    //
  2367.    vector<int> v3(d2+0, d2+8), v4(d2+0, d2+8), v5(d2+0, d2+8), v6(d2+0, d2+8);
  2368.    //
  2369.    // Set up one empty vector.
  2370.    //
  2371.    vector<int> v7;
  2372.    vector<int> vResult;
  2373.    vector<int>::iterator vItr;
  2374.    //
  2375.    // Merge v1 with v2.
  2376.    //
  2377.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
  2378.    //
  2379.    // Now use comparator.
  2380.    //
  2381.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin(), less<int>());
  2382.    //
  2383.    // In place merge v5.
  2384.    //
  2385.    vector<int>::iterator mid = v5.begin();
  2386.    advance(mid,4);
  2387.    inplace_merge(v5.begin(),mid,v5.end());
  2388.    //
  2389.    // Now use a comparator on v6.
  2390.    //
  2391.    mid = v6.begin();
  2392.    advance(mid,4);
  2393.    inplace_merge(v6.begin(), mid, v6.end(), less<int>());
  2394.    //
  2395.    // Merge v1 and v2 to empty vector using insert iterator.
  2396.    //
  2397.    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v7));
  2398.  
  2399.    for(vItr = v1.begin(); vItr != v1.end(); vItr++)
  2400.      Form1->Memo1->Lines->Strings[ct] =
  2401.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2402.    ct++;
  2403.    for(vItr = v2.begin(); vItr != v2.end(); vItr++)
  2404.      Form1->Memo1->Lines->Strings[ct] =
  2405.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2406.    ct++;
  2407.    for(vItr = v3.begin(); vItr != v3.end(); vItr++)
  2408.      Form1->Memo1->Lines->Strings[ct] =
  2409.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2410.    ct++;
  2411.    for(vItr = v4.begin(); vItr != v4.end(); vItr++)
  2412.      Form1->Memo1->Lines->Strings[ct] =
  2413.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2414.    ct++;
  2415.    for(vItr = v5.begin(); vItr != v5.end(); vItr++)
  2416.      Form1->Memo1->Lines->Strings[ct] =
  2417.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2418.    ct++;
  2419.    for(vItr = v6.begin(); vItr != v6.end(); vItr++)
  2420.      Form1->Memo1->Lines->Strings[ct] =
  2421.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2422.    ct++;
  2423.    for(vItr = v7.begin(); vItr != v7.end(); vItr++)
  2424.      Form1->Memo1->Lines->Strings[ct] =
  2425.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2426.    ct++;
  2427.    //
  2428.    // Merge v1 and v2 .
  2429.    //
  2430.    merge(v1.begin(),v1.end(),v2.begin(),v2.end(),
  2431.            inserter(vResult, vResult.begin()));
  2432.    for(vItr = vResult.begin(); vItr != vResult.end(); vItr++)
  2433.      Form1->Memo1->Lines->Strings[ct] =
  2434.        Form1->Memo1->Lines->Strings[ct] + IntToStr(*vItr) + " ";
  2435.    ct++;
  2436.  
  2437.    return 0;
  2438.  }
  2439.  
  2440.  
  2441.  int pqueue_ex ()
  2442.  {
  2443.    //
  2444.    // Make a priority queue of int  using a deque container.
  2445.    //
  2446.    priority_queue<int, vector<int>, less<int> > pq;
  2447.    //
  2448.    // Push a couple of values.
  2449.    //
  2450.    Form1->Memo1->Lines->Strings[ct++] = " ========= Priority Queue Example =====";
  2451.    pq.push(1);
  2452.    pq.push(2);
  2453.    //
  2454.    // Pop a couple of values and examine the ends.
  2455.    //
  2456.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(pq.top()) ;
  2457.    pq.pop();
  2458.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(pq.top());
  2459.    pq.pop();
  2460.    //
  2461.    // Make a priority queue of strings.
  2462.    //
  2463.    priority_queue<string,deque<string>, less<string> > pqs;
  2464.    //
  2465.    // Push on a few strings then pop them back off.
  2466.    //
  2467.    int i;
  2468.    for (i = 0; i < 10; i++)
  2469.    {
  2470.      pqs.push(string(i+1,'a'));
  2471.      Form1->Memo1->Lines->Strings[ct++] = (String)(pqs.top()).data() ;
  2472.    }
  2473.    for (i = 0; i < 10; i++)
  2474.    {
  2475.      Form1->Memo1->Lines->Strings[ct++] = (String)(pqs.top()).data() ;
  2476.      pqs.pop();
  2477.    }
  2478.    //
  2479.    // Make a priority queue of strings using greater.
  2480.    //
  2481.    priority_queue<string,deque<string>, greater<string> > pgqs;
  2482.    //
  2483.    // Push on a few strings then pop them back off.
  2484.    //
  2485.    for (i = 0; i < 10; i++)
  2486.    {
  2487.      pgqs.push(string(i+1,'a'));
  2488.      Form1->Memo1->Lines->Strings[ct++] = (String)(pgqs.top()).data() ;
  2489.    }
  2490.    for (i = 0; i < 10; i++)
  2491.    {
  2492.      Form1->Memo1->Lines->Strings[ct++] = (String)(pgqs.top()).data() ;
  2493.      pgqs.pop();
  2494.    }
  2495.  
  2496.    return 0;
  2497.  }
  2498.  
  2499.  int rev_itr_ex()
  2500.  {
  2501.    //
  2502.    // Initialize a vector using an array.
  2503.    //
  2504.    int arr[4] = {3,4,7,8};
  2505.    vector<int> v(arr+0, arr+4);
  2506.    //
  2507.    // Output the original vector.
  2508.    //
  2509.    Form1->Memo1->Lines->Strings[ct++] = " ========= Reverse Iterator Example =====";
  2510.    Form1->Memo1->Lines->Strings[ct++] = "Traversing vector with iterator:" ;
  2511.    for(vector<int>::iterator i = v.begin(); i != v.end(); i++)
  2512.      Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(*i) + " ";
  2513.    ct++;
  2514.    //
  2515.    // Declare the reverse_iterator.
  2516.    //
  2517.    vector<int>::reverse_iterator rev(v.end());
  2518.    vector<int>::reverse_iterator rev_end(v.begin());
  2519.    //
  2520.    // Output the vector backwards.
  2521.    //
  2522.    Form1->Memo1->Lines->Strings[ct++] = "Same vector, same loop, reverse_iterator: " ;
  2523.    for(; rev != rev_end; rev++)
  2524.      Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(*rev) + " ";
  2525.    ct++;
  2526.    return 0;
  2527.  }
  2528.  
  2529.  
  2530. int sieve_ex() {
  2531.     Form1->Memo1->Lines->Strings[ct++] = "======= Prime Sieve example, using vectors ==";
  2532.  
  2533.     // create a sieve of bits, initially on
  2534.     const int sievesize = 100;
  2535.     vector<int> sieve(sievesize, 1);
  2536.  
  2537.         // now search for 1 bt positions
  2538.     for (int i = 2; i * i < sievesize; i++)
  2539.         if (sieve[i])
  2540.             for (int j = i + i; j < sievesize; j += i)
  2541.                 sieve[j] = 0;
  2542.  
  2543.         // now output all the values that are set
  2544.     for (int j = 2; j < sievesize; j++)
  2545.         if (sieve[j])
  2546.             Form1->Memo1->Lines->Strings[ct] = Form1->Memo1->Lines->Strings[ct] + IntToStr(j) + " ";
  2547.     ct++;
  2548.  
  2549.     Form1->Memo1->Lines->Strings[ct++] = "End of Prime Sieve program" ;
  2550.     return 0;
  2551. }
  2552.  
  2553.  
  2554. int stack_ex ()
  2555. {
  2556.    //
  2557.    // Make a stack using a vector container.
  2558.    //
  2559.    stack<int,vector<int> > s;
  2560.    //
  2561.    // Push a couple of values on the stack.
  2562.    //
  2563.    Form1->Memo1->Lines->Strings[ct++] = "============= Stack example ========";
  2564.    s.push(1);
  2565.    s.push(2);
  2566.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(s.top()) ;
  2567.    //
  2568.    // Now pop them off.
  2569.    //
  2570.    s.pop();
  2571.    Form1->Memo1->Lines->Strings[ct++] = IntToStr(s.top());
  2572.    s.pop();
  2573.    //
  2574.    // Make a stack of strings using a deque.
  2575.    //
  2576.    stack<string,deque<string> > ss;
  2577.    //
  2578.    // Push a bunch of strings on then pop them off.
  2579.    //
  2580.    int i;
  2581.    for (i = 0; i < 10; i++)
  2582.    {
  2583.      ss.push(string(i+1,'a'));
  2584.      Form1->Memo1->Lines->Strings[ct++] = (String)(ss.top()).data() ;
  2585.    }
  2586.    for (i = 0; i < 10; i++)
  2587.    {
  2588.      Form1->Memo1->Lines->Strings[ct++] = (String)(ss.top()).data() ;
  2589.      ss.pop();
  2590.    }
  2591.  
  2592.    return 0;
  2593.  }
  2594.  
  2595. // begin telephone example source
  2596.  
  2597. typedef map<string, long, less<string> > friendMap;
  2598. typedef map<long, string, less<long> >   sortedMap;
  2599.  
  2600. //
  2601. //    utility functions used in telephone directory
  2602. //
  2603.  
  2604. typedef friendMap::value_type entry_type;
  2605. typedef sortedMap::value_type sorted_entry_type;
  2606.  
  2607. void printEntry(const entry_type & entry)
  2608.     { Form1->Memo1->Lines->Strings[ct++] = (String)entry.first.data() + ":" + IntToStr(entry.second) ; }
  2609.  
  2610. void printSortedEntry(const sorted_entry_type & entry)
  2611.     { Form1->Memo1->Lines->Strings[ct++] = IntToStr(entry.first) + ":" + (String)entry.second.data();  }
  2612.  
  2613. int prefix(const entry_type& entry)
  2614.     { return entry.second / 10000; }
  2615.  
  2616. bool prefixCompare(const entry_type & a, const entry_type & b)
  2617.     { return prefix(a) < prefix(b); }
  2618.  
  2619. class checkPrefix {
  2620. public:
  2621.     checkPrefix (int p) : testPrefix(p) { }
  2622.     int testPrefix;
  2623.     bool operator () (const entry_type& entry)
  2624.         { return prefix(entry) == testPrefix; }
  2625. };
  2626.  
  2627. class telephoneDirectory {
  2628. public:
  2629.     void addEntry (string name, long number)
  2630.         { database[name] = number; }
  2631.  
  2632.     void remove (string name)
  2633.         { database.erase(name); }
  2634.  
  2635.     void update (string name, long number)
  2636.         { remove(name); addEntry(name, number); }
  2637.  
  2638.     void displayDatabase()
  2639.         { for_each(database.begin(), database.end(), printEntry); }
  2640.  
  2641.     void displayPrefix(int);
  2642.  
  2643.     void displayByPrefix();
  2644.  
  2645. private:
  2646.     friendMap database;
  2647. };
  2648.  
  2649. void telephoneDirectory::displayPrefix(int prefix)
  2650. {
  2651.     Form1->Memo1->Lines->Strings[ct++] = "Listing for prefix " + IntToStr(prefix) ;
  2652.     map<string, long, less<string> >::iterator where;
  2653.     where = find_if(database.begin(), database.end(), checkPrefix(prefix));
  2654.     while (where != database.end()) {
  2655.         printEntry(*where);
  2656.         where = find_if(++where, database.end(), checkPrefix(prefix));
  2657.         }
  2658.     Form1->Memo1->Lines->Strings[ct++] = "end of prefix listing" ;
  2659. }
  2660.  
  2661. void telephoneDirectory::displayByPrefix()
  2662. {
  2663.     Form1->Memo1->Lines->Strings[ct++] = "Display by prefix" ;
  2664.  
  2665.     sortedMap sortedData;
  2666.  
  2667.     for (friendMap::iterator i = database.begin(); i != database.end(); i++)
  2668.         sortedData.insert(sortedMap::value_type((*i).second,
  2669.                             (*i).first));
  2670.  
  2671.     for_each(sortedData.begin(), sortedData.end(), printSortedEntry);
  2672.     Form1->Memo1->Lines->Strings[ct++] = "end display by prefix" ;
  2673. }
  2674.  
  2675. int telephone_ex() {
  2676.     Form1->Memo1->Lines->Strings[ct++] = "==== Telephone Directory sample program" ;
  2677.     telephoneDirectory friends;
  2678.  
  2679.     friends.addEntry("Samantha", 6342343);
  2680.     friends.addEntry("Brenda", 5436546);
  2681.     friends.addEntry("Fred", 7435423);
  2682.     friends.addEntry("Allen", 6348723);
  2683.  
  2684.     friends.displayDatabase();
  2685.     friends.displayPrefix(634);
  2686.     friends.displayByPrefix();
  2687.  
  2688.     Form1->Memo1->Lines->Strings[ct++] = "End of telephone directory sample program" ;
  2689.     return 0;
  2690. }
  2691. 
  2692.