home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 February
/
Chip_2002-02_cd1.bin
/
chplus
/
funktor
/
f14_stl_ptr_fun.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2002-01-02
|
4KB
|
91 lines
//---------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath> // std::sqrt
//---------------------------------------------------------------------------
// soubor: f14_stl_ptr_fun.cpp
// autor: Jaroslav Franek
// vytvoreno: 20011123
// copyright: (c) 2001 Jaroslav Franek
// umisteni: Chip CD, rubrika Chip Plus, 02/02
// reference: Jak se na funktor vola... II. dil, Chip 02/02
// testovano v : Borland C++ Builder 5.0
// Microsoft Visual C++ .NET 7.0 (beta2)
//
// komentar: stl adaptery pointer_to_unary_function,
// pointer_to_binary_function,
// vytvorujici funkce ptr_fun
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// ruzne zpusoby pouziti v akci
//
//---------------------------------------------------------------------------
int main()
{
// nejdrive si pripravime kontejnery
const int N = 9;
int pole[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// a ted STL kontejnery
std::vector<double> cisla1(pole, pole + N); // N prvku, pole[i]
std::vector<double> cisla2(N, 0.0); // N prvku, kazdy 0.0
// vypiseme si je na obrazovku,
std::cout << "cisla1 : "; // vypisujeme cisla typu double
std::copy(cisla1.begin(), cisla1.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << "\n"
"cisla2 : ";
std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
//*******************************************************************
// cisla2 = sqrt(cisla1), po slozkach, zatim bez niceho
//
std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::sqrt);
//*******************************************************************
// vypiseme
std::cout << "klasicka funkce" << std::endl;
std::cout << "sqrt(cisla1) : ";
std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
//*******************************************************************
// cisla2 = sqrt(cisla1), po slozkach, s adapterem
//
std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::pointer_to_unary_function<double ,double>(std::sqrt));
//******************************************************************* // slo by napsat i ...(&std::sqrt));
// ale konverze na ukazatel je automaticka
// vypiseme // podobne jako u klasickych poli
std::cout << "adapter" << std::endl;
std::cout << "sqrt(cisla1) : ";
std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
//*******************************************************************
// cisla2 = sqrt(cisla1), po slozkach, s vytvorujici funkci
//
std::transform(cisla1.begin(), cisla1.end(), cisla2.begin(), std::ptr_fun(std::sqrt));
//******************************************************************* // ..(&std::sqrt)); // tady taky, viz pozn vyse
// vypiseme
std::cout << "vytvorujici funkce" << std::endl;
std::cout << "sqrt(cisla1) : ";
std::copy(cisla2.begin(), cisla2.end(), std::ostream_iterator<double>(std::cout, " "));
std::cout << std::endl;
// vsechny tri vysledky by mely byt stejne
// spravne hodnoty si jiste dokazete overit sami
return 0;
}
//---------------------------------------------------------------------------