home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / Chip_2002-02_cd1.bin / chplus / funktor / f14_stl_ptr_fun.cpp < prev    next >
C/C++ Source or Header  |  2002-01-02  |  4KB  |  91 lines

  1. //---------------------------------------------------------------------------
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <functional>
  7. #include <cmath>         // std::sqrt
  8.  
  9. //---------------------------------------------------------------------------
  10. //  soubor:    f14_stl_ptr_fun.cpp
  11. //  autor:     Jaroslav Franek
  12. //  vytvoreno: 20011123
  13. //  copyright: (c) 2001 Jaroslav Franek
  14. //  umisteni:  Chip CD, rubrika Chip Plus, 02/02
  15. //  reference: Jak se na funktor vola... II. dil, Chip 02/02
  16. //  testovano v : Borland C++ Builder 5.0
  17. //                Microsoft Visual C++ .NET 7.0 (beta2)
  18. //
  19. //  komentar:  stl adaptery pointer_to_unary_function,
  20. //             pointer_to_binary_function,
  21. //             vytvorujici funkce ptr_fun
  22. //---------------------------------------------------------------------------
  23.  
  24. //---------------------------------------------------------------------------
  25. //
  26. //  ruzne zpusoby pouziti v akci
  27. //
  28. //---------------------------------------------------------------------------
  29.  
  30. int main()
  31. {
  32.    // nejdrive si pripravime kontejnery
  33.    const int N = 9;
  34.    int pole[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  35.  
  36.    // a ted STL kontejnery
  37.    std::vector<double> cisla1(pole, pole + N); // N prvku, pole[i]
  38.    std::vector<double> cisla2(N, 0.0);         // N prvku, kazdy 0.0
  39.  
  40.    // vypiseme si je na obrazovku,
  41.    std::cout << "cisla1 : ";                        // vypisujeme cisla typu double
  42.    std::copy(cisla1.begin(), cisla1.end(), std::ostream_iterator<double>(std::cout, "  "));
  43.    std::cout << "\n"
  44.                 "cisla2 : ";
  45.    std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, "  "));
  46.    std::cout << std::endl;
  47.  
  48. //*******************************************************************
  49.    //  cisla2 = sqrt(cisla1), po slozkach, zatim bez niceho
  50.    //
  51.    std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::sqrt);
  52. //*******************************************************************
  53.  
  54.    // vypiseme
  55.    std::cout << "klasicka funkce" << std::endl;
  56.    std::cout << "sqrt(cisla1) : ";
  57.    std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, "  "));
  58.    std::cout << std::endl;
  59.  
  60. //*******************************************************************
  61.    //  cisla2 = sqrt(cisla1), po slozkach, s adapterem
  62.    //
  63.    std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::pointer_to_unary_function<double ,double>(std::sqrt));
  64. //*******************************************************************   // slo by napsat i                 ...(&std::sqrt));
  65.                                                                         // ale konverze na ukazatel je automaticka
  66.    // vypiseme                                                          // podobne jako u klasickych poli
  67.    std::cout << "adapter" << std::endl;
  68.    std::cout << "sqrt(cisla1) : ";
  69.    std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, "  "));
  70.    std::cout << std::endl;
  71.  
  72. //*******************************************************************
  73.    //  cisla2 = sqrt(cisla1), po slozkach, s vytvorujici funkci
  74.    //
  75.    std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::ptr_fun(std::sqrt));
  76. //*******************************************************************  // ..(&std::sqrt));  // tady taky, viz pozn vyse
  77.  
  78.    // vypiseme
  79.    std::cout << "vytvorujici funkce" << std::endl;
  80.    std::cout << "sqrt(cisla1) : ";
  81.    std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, "  "));
  82.    std::cout << std::endl;
  83.  
  84.    // vsechny tri vysledky by mely byt stejne
  85.    // spravne hodnoty si jiste dokazete overit sami
  86.  
  87.    return 0;
  88. }
  89. //---------------------------------------------------------------------------
  90.  
  91.