Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- [](int broj)
- {
- //int broj=5;
- cout << "Broj " << broj << " je";
- if (broj % 2 == 0)
- cout << "paran" << endl;
- else
- cout << " neparan" << endl;
- }(5); //pozivamo lambda funkciju i šaljemo joj broj 5
- system("PAUSE");
- return 0;
- }
- /////
- #include <iostream>
- using namespace std;
- //Primjer lambda funkcije izračunavasumu vrijednosti članova niza
- int main()
- {
- const int max = 5;
- int niz[] = { 3,9,1,56,83 };
- int suma = [](int niz[], int max)
- {
- int sum = 0;
- for (int i = 0; i < max; i++)
- {
- sum +=niz[i];
- return sum;
- }
- }(niz, max);//funkciji prosljeđujemo niz i max
- cout << "Suma= " << suma << endl;
- system("PAUSE");
- return 0;
- }
- /////
- /*unutar lambda funkcije dostupne dostupne vrijednost iz opsega u kome
- je funkcija kreirana onda ih navodimo u dijelu lista_parametara_iz_opsega */
- #include <iostream>
- using namespace std;
- int main()
- {
- const int max = 5;
- int niz[] = { 3,9,1,56,83 };
- int suma = [niz, max]()
- {
- int sum = 0;
- for (int i = 0; i < max; i++)
- {
- sum += niz[i];
- return sum;
- }
- }();//funkciji ne prosljeđujemo niz i max tj. parametre
- cout << "Suma= " << suma << endl;
- system("PAUSE");
- return 0;
- }
- /////
- #include <iostream>
- using namespace std;
- int main()
- {
- int(*pok)[int [], int] = [](int niz[], int max)->int
- {
- int sum = 0;
- for (int i = 0; i < max; i++)
- sum += niz[i];
- return sum;
- };
- cout << "Suma= " << pok(niz, max) << endl;
- niz[1] = 10;
- cout << "Suma= " << pok(niz, max) << endl;
- system("PAUSE");
- return 0;
- }
- /////
- #include <iostream>
- #include <functional>
- using namespace std;
- void main()
- {
- int max = 5;
- auto pok1 = [max]()
- {
- return max < 10;
- };
- cout << "Velicina niza je manja od 10= " << pok1() << endl;
- //cuvamo pokazivac na funkciju koja vraca bool i ne prima parametre
- function<bool()>pok2 = [max]()
- {
- return max < 10;
- };
- cout << "Velicina niza je manje od 10= "<< pok2() << endl;
- system("PAUSE");
- }
- /////
- //kreiranje lambda rekurzivne funkcije koja izračunava sumu brojeva od 1 do 10
- #include <iostream>
- using namespace std;
- void main()
- {
- static int(*rekFunkcija)(int) = [](int broj)->int
- {
- if (broj <= 0)
- return broj;
- return broj + rekFunkcija(broj - 1);
- };
- cout << rekFunkcija(10) << endl;
- system("PAUSE");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement