home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / SAMPLES / ICLCC / SUMUP.C < prev    next >
C/C++ Source or Header  |  1993-05-07  |  3KB  |  75 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* COPYRIGHT:                                                                 */
  4. /* ----------                                                                 */
  5. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  6. /*                                                                            */
  7. /* DISCLAIMER OF WARRANTIES:                                                  */
  8. /* -------------------------                                                  */
  9. /* The following [enclosed] code is sample code created by IBM                */
  10. /* Corporation.  This sample code is not part of any standard IBM product     */
  11. /* and is provided to you solely for the purpose of assisting you in the      */
  12. /* development of your applications.  The code is provided "AS IS",           */
  13. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  14. /* arising out of your use of the sample code, even if they have been         */
  15. /* advised of the possibility of such damages.                                */
  16. /*                                                                            */
  17. /******************************************************************************/
  18. /*-------------------------------------------------------------*\
  19. |  sumup.C  - Sum up integers to demonstrate using iterators.   |
  20. |                                                  """""""""    |
  21. |  Add all elements in a bag to demonstrate the use of          |
  22. |  iterators with additional arguments.                         |
  23. |  The bag elements are integers passed as arguments to main(). |
  24. \*-------------------------------------------------------------*/
  25.  
  26.    #include <ibag.h>
  27.    #include <iostream.h>
  28.  
  29.    typedef IBag < int > IntBag;
  30.  
  31.    class SumIterator : public IConstantIterator < int > {
  32.      int ivSum;
  33.    public:
  34.      SumIterator () : ivSum (0) {}
  35.      Boolean applyTo (int const& i) {
  36.        ivSum += i;
  37.        return True;
  38.      }
  39.      int sum () { return ivSum; }
  40.    };
  41.  
  42.  
  43.    int sumUsingIteratorObject (IntBag const& bag) {
  44.      SumIterator sumUp;
  45.      bag.allElementsDo (sumUp);
  46.      return sumUp.sum ();
  47.    }
  48.  
  49.  
  50.    Boolean sumUpFunction (int const& i, void* sum) {
  51.      *(int*)sum += i;
  52.      return True;
  53.    };
  54.  
  55.    int sumUsingIteratorFunction (IntBag const& bag) {
  56.      int sum = 0;
  57.      bag.allElementsDo (sumUpFunction, &sum);
  58.      return sum;
  59.    }
  60.  
  61.  
  62. int main (int argc, char* argv[])  {
  63.      IntBag intbag;
  64.      for (int cnt=1; cnt < argc; cnt++)
  65.         intbag.add(atoi(argv[cnt]));
  66.  
  67.      cout << "Sum obtained using an Iterator Object = "
  68.           <<  sumUsingIteratorObject(intbag)  << "\n";
  69.  
  70.      cout << "Sum obtained using an Iterator Function = "
  71.           <<  sumUsingIteratorFunction(intbag)  << "\n";
  72.  
  73.      return 0;
  74.      }
  75.