cout<<"Int_massiv da element topilmadi \n";
vector myvector (Int_massiv,Int_massiv+4);
vector::iterator it;
it = find (myvector.begin(), myvector.end(), 30);
if (it != myvector.end())
cout<<"myvector da element topildi: "<< *it <<'\n';
else
cout << "myvector da element topilmadi \n";
getchar();
return 0;
}
Dastur natijasi:
Int_massiv da element topilgan: 30
myvector da element topildi: 30
mismatch shabloni:
template
pair
mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for version 2
{ ++first1; ++first2; }
return std::make_pair(first1,first2);
}
mismatch(a, a_end, b, b_end, eq) algoritmiga misol:
#include "stdafx.h"
#include // std::cout
#include // std::mismatch
#include // std::vector
#include // std::pair
bool mypredicate (int i, int j) {
return (i==j);
}
using namespace std;
int main ()
{
vector myvector;
for (int i=1; i<6; i++) myvector.push_back (i*10);
// myvector: 10 20 30 40 50
int Int_massiv[] = {10,20,80,320,1024};
// Int_massiv: 10 20 80 320 1024
pair::iterator,int*> mypair;
// standart taqqoslash yordamida:
mypair = mismatch (myvector.begin(), myvector.end(), Int_massiv);
cout << "Birinchi mos kelmaydigan elementlar: " << *mypair.first;
cout << " va " << *mypair.second << '\n';
++mypair.first; ++mypair.second;
// predikt taqqoslash yordamida:
mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);
cout << "Ikkinchi mos kelmaydigan elementlar: " << *mypair.first;
cout << " va " << *mypair.second << '\n';
getchar();
return 0;
}
Dastur natijasi:
Birinchi mos kelmaydigan elementlar: 30 va 80
Ikkinchi mos kelmaydigan elementlar: 40 va 320
max_element shabloni:
template
ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
if (first==last) return last;
ForwardIterator largest = first;
while (++first!=last)
if (*largest<*first) // or: if (comp(*largest,*first)) for version (2)
largest=first;
return largest;
}
max_element(begin, end, comp) va min_element(begin, end, comp) algoritmiga misol:
#include "stdafx.h"
#include // std::cout
#include // std::min_element, std::max_element
bool myfn(int i, int j) { return i
struct myclass {
bool operator() (int i,int j) { return i
} myobj;
using namespace std;
int main () {
int Int_massiv[] = {3,7,2,5,6,4,9};
// standart taqqoslash yordamida:
cout << "Eng kichik element - " << *min_element(Int_massiv,Int_massiv+7) << '\n';
cout << "Eng katta element - " << *max_element(Int_massiv,Int_massiv+7) << '\n';
// myfn funktsiyasidan komp sifatida foydalanish:
cout << "Eng kichik element " << *min_element(Int_massiv,Int_massiv+7,myfn) << '\n';
cout << "Eng katta element " << *max_element(Int_massiv,Int_massiv+7,myfn) << '\n';
// obyekt sifatida myobj-dan komp sifatida foydalanish:
cout << "Eng kichik element " << *min_element(Int_massiv,Int_massiv+7,myobj) << '\n';
cout << "Eng katta element " << *max_element(Int_massiv,Int_massiv+7,myobj) << '\n';
getchar();
return 0;
}
Dastur natijasi:
Eng kichik element - 2
Eng katta element - 9
Eng kichik element 2
Eng katta element 9
Eng kichik element 2
Eng katta element 9
For_each shabloni:
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) {
fn (*first);
++first;
}
return fn; // or, since C++11: return move(fn);
}
For_each(begin, end, fun) algoritmiga misol:
#include "stdafx.h"
#include // std::cout
#include // std::for_each
#include // std::vector
void myfunction (int i) { // function:
std::cout << ' ' << i;
}
struct myclass { // function object type:
void operator() (int i) {std::cout << ' ' << i;}
} myobject;
using namespace std;
int main () {
vector myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
cout << "myvector tarkibi:";
for_each (myvector.begin(), myvector.end(), myfunction);
cout << '\n';
cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);
cout << '\n';
getchar();
return 0;
}
Dastur natijasi:
myvector tarkibi: 10 20 30
myvector contains: 10 20 30
replace shabloni:
template
void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
replace(begin, end, old_val, new_val) algoritmiga misol:
#include "stdafx.h"
#include // std::cout
#include // std::replace
#include // std::vector
using namespace std;
int main () {
int Int_massiv[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
vector myvector (Int_massiv, Int_massiv+8);
// 10 20 30 30 20 10 10 20
replace (myvector.begin(), myvector.end(), 20, 99);
// 10 99 30 30 99 10 10 99
cout << "myvector tarkibi:";
for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
getchar();
return 0;
}
Dastur natijasi: myvector tarkibi: 10 99 30 30 99 10 10 99
remove shabloni:
template
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
*result = *first;
++result;
}
++first;
}
return result;
}
Remove_if shabloni:
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator remove_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred)
{
ForwardIterator result = first;
while (first!=last) {
if (!pred(*first)) {
*result = *first;
++result;
}
++first;
}
return result;
}
remove va remove_if(begin, end, pred) algorimiga misol:
#include "stdafx.h"
#include // std::cout
#include // std::remove va remove_if
bool IsOdd (int i) { return ((i%2)==1); }
using namespace std;
int main () {
int Int_massiv[]={1,2,3,4,5,6,7,8,9};
// 3 2 3 4 5 6 3 8 9
//Chegaralar:
int* pbegin = Int_massiv; // ^
int* pend = Int_massiv+sizeof(Int_massiv)/sizeof(int); // ^ ^
pend = remove_if (pbegin, pend, IsOdd);
// 2 4 6 8 ? ? ? ? ?
cout << " remove_if dan keyin Range trakibi:";
for (int* p=pbegin; p!=pend; ++p)
cout << ' ' << *p;
cout << '\n';
int Int_massiv_2[] = {3,2,3,4,3,6,7,3,9};
pbegin = Int_massiv_2;
int* pend_2 = Int_massiv_2+sizeof(Int_massiv_2)/sizeof(int);
pend_2 = remove (pbegin, pend_2, 3);
cout << " remove dan keyin Range trakibi:";
for (int* p=pbegin; p!=pend_2; ++p)
cout << ' ' << *p;
cout << '\n';
getchar();
return 0;
}
Dastur natijasi:
remove_if dan keyin Range trakibi: 2 4 6 8
remove dan keyin Range trakibi: 2 4 6 7 9
sort shabloni:
template
void sort (RandomAccessIterator first, RandomAccessIterator last);
sort(begin, end, comp) algoritmiga misol:
#include // std::cout
#include // std::sort
#include // std::vector
bool myfunction (int i,int j) { return (i
}
struct myclass
{
bool operator() (int i,int j) { return (i
} myobject;
using namespace std;
int main ()
{
int Int_massiv[] = {32,71,12,45,26,80,53,33};
vector myvector (Int_massiv, Int_massiv+8);
// 32 71 12 45 26 80 53 33
// standart taqqoslash (operator <) yordamida:
sort (myvector.begin(), myvector.begin()+4);
//(12 32 45 71)26 80 53 33
// funksiyadan foydalanib
sort (myvector.begin()+4, myvector.end(), myfunction);
// 12 32 45 71(26 33 53 80)
// objectdan foydalib
sort (myvector.begin(), myvector.end(), myobject);
//(12 26 32 33 45 53 71 80)
// elementlarni chop qilish:
cout << "myvector tarkitibi:";
for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
getchar();
return 0;
}
Dastur natijasi:
myvector tarkitibi: 12 26 32 33 45 53 71 80
|
Do'stlaringiz bilan baham: |