Guruh talabasi Qurbonboyev Shokirbekning 1- tajriba ishi Mavzu: Funksiya va sinflar shablonlari. Nazariy qism Funksiyalar shablonlari


Download 37.85 Kb.
bet4/4
Sana02.06.2024
Hajmi37.85 Kb.
#1835950
1   2   3   4
Bog'liq
1 laboratoriya ishi

for ( int j = n-1; i

if (cmp.lessthan(v[j],v[j-1])) {

T temp = v[j];

v[j] = v[j-1];

v[j-1] = temp;

}

}


for_each() algoritm yordamida har xil kо‘rinishidagi qayta ishlashni va har bir elementni modifikatsiyasini kо‘rish mumkin.




Template

void for_each(vector a, function op)

{

for( int i=0; i

}


for_each() algoritmining qо‘llanishi:

# include

class StudentPrint

{public:

void operator() (Student elem){ if (elem.rating>5) elem.print();}

};

main()

{

vector coll(5);

coll.input();

StudentPrint cmp;

for_each(coll, cmp);

}




1)

N ta elementdan tashkil topgan massiv berilgan. Massiv elementlari orasidan oxirgi lokal maksimum elementi indeksini chiqaruvchi programma tuzilsin. Lokal maksimum - o’ng va chap qo'shinisidan katta bo'lgan element.


#include


using namespace std;
template
TypeName Solution(TypeName a[], int n) {
TypeName Max = a[1];
int index = 1;
for(int i = 1; i < n-1; i++){
if (a[i-1] < a[i] && a[i] > a[i+1]){
Max = a[i];
index = i;
}}
return Max;}
int main () {
int a[] = {1, 2, 1, 3, 1, 2, 1, 1, 1, 2};
double b[] = {1.2, 2, 1, 3, 1, 2.4, 1, 22.5, 10.8, 2};
int n = sizeof(a)/sizeof(int);
cout<<"int arr = "<cout<<"double arr = "<return 0;}


Download 37.85 Kb.

Do'stlaringiz bilan baham:

1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling