Advertisement
limun11

PRII- Lambda funkcije P7

Apr 25th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     [](int broj)
  7.     {
  8.         //int broj=5;
  9.         cout << "Broj " << broj << " je";
  10.         if (broj % 2 == 0)
  11.             cout << "paran" << endl;
  12.         else
  13.             cout << " neparan" << endl;
  14.     }(5); //pozivamo lambda funkciju i šaljemo joj broj 5
  15.     system("PAUSE");
  16.     return 0;
  17. }
  18.  
  19. /////
  20.  
  21. #include <iostream>
  22. using namespace std;
  23. //Primjer lambda funkcije  izračunavasumu vrijednosti članova niza
  24. int main()
  25. {
  26.     const int max = 5;
  27.     int niz[] = { 3,9,1,56,83 };
  28.  
  29.     int suma = [](int niz[], int max)
  30.     {
  31.         int sum = 0;
  32.         for (int i = 0; i < max; i++)
  33.         {
  34.             sum +=niz[i];
  35.             return sum;
  36.         }
  37.     }(niz, max);//funkciji prosljeđujemo niz i max
  38.     cout << "Suma= " << suma << endl;
  39.     system("PAUSE");
  40.     return 0;
  41. }
  42.  
  43. /////
  44.  
  45. /*unutar lambda funkcije dostupne dostupne vrijednost iz opsega u kome
  46. je funkcija kreirana onda ih navodimo u dijelu lista_parametara_iz_opsega */
  47.  
  48. #include <iostream>
  49. using namespace std;
  50.  
  51. int main()
  52. {
  53.     const int max = 5;
  54.     int niz[] = { 3,9,1,56,83 };
  55.  
  56.     int suma = [niz, max]()
  57.     {
  58.         int sum = 0;
  59.         for (int i = 0; i < max; i++)
  60.         {
  61.             sum += niz[i];
  62.             return sum;
  63.         }
  64.     }();//funkciji ne prosljeđujemo niz i max tj. parametre
  65.     cout << "Suma= " << suma << endl;
  66.     system("PAUSE");
  67.     return 0;
  68. }
  69.  
  70. /////
  71.  
  72. #include <iostream>
  73. using namespace std;
  74.  
  75. int main()
  76. {
  77.     int(*pok)[int [], int] = [](int niz[], int max)->int
  78.     {
  79.         int sum = 0;
  80.         for (int i = 0; i < max; i++)
  81.             sum += niz[i];
  82.         return sum;
  83.     };
  84.     cout << "Suma= " << pok(niz, max) << endl;
  85.     niz[1] = 10;
  86.     cout << "Suma= " << pok(niz, max) << endl;
  87.     system("PAUSE");
  88.     return 0;
  89. }
  90.  
  91. /////
  92.  
  93. #include <iostream>
  94. #include <functional>
  95. using namespace std;
  96.  
  97. void main()
  98. {
  99.     int max = 5;
  100.  
  101.     auto pok1 = [max]()
  102.     {
  103.         return max < 10;
  104.     };
  105.     cout << "Velicina niza je manja od 10= " << pok1() << endl;
  106.  
  107.     //cuvamo pokazivac na funkciju koja vraca bool i ne prima parametre
  108.     function<bool()>pok2 = [max]()
  109.     {
  110.         return max < 10;
  111.     };
  112.     cout << "Velicina niza je manje od 10= "<< pok2() << endl;
  113.  
  114.     system("PAUSE");
  115.    
  116. }
  117.  
  118. /////
  119.  
  120. //kreiranje lambda rekurzivne funkcije koja izračunava sumu brojeva od 1 do 10
  121.  
  122. #include <iostream>
  123. using namespace std;
  124.  
  125. void main()
  126. {
  127.     static int(*rekFunkcija)(int) = [](int broj)->int
  128.     {
  129.         if (broj <= 0)
  130.             return broj;
  131.         return broj + rekFunkcija(broj - 1);
  132.     };
  133.     cout << rekFunkcija(10) << endl;
  134.     system("PAUSE");
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement