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: class StudentPrint {public: void operator() (Student elem){ if (elem.rating>5) elem.print();} }; main() { vector coll(5); coll.input(); 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;}
Do'stlaringiz bilan baham: |