1- laboratoriya ishi konteynerlar (collections)



Download 89,18 Kb.
bet20/20
Sana20.03.2022
Hajmi89,18 Kb.
#502935
1   ...   12   13   14   15   16   17   18   19   20
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:

    1. #include "stdafx.h"

    2. #include // std::cout

    3. #include // std::mismatch

    4. #include // std::vector

    5. #include // std::pair




    1. bool mypredicate (int i, int j) {

    2. return (i==j);

    3. }

    4. using namespace std;

    5. int main ()

    6. {

    7. vector myvector;

    8. for (int i=1; i<6; i++) myvector.push_back (i*10);

    9. // myvector: 10 20 30 40 50




    1. int Int_massiv[] = {10,20,80,320,1024};

    2. // Int_massiv: 10 20 80 320 1024




    1. pair::iterator,int*> mypair;




    1. // standart taqqoslash yordamida:

    2. mypair = mismatch (myvector.begin(), myvector.end(), Int_massiv);

    3. cout << "Birinchi mos kelmaydigan elementlar: " << *mypair.first;

    4. cout << " va " << *mypair.second << '\n';




    1. ++mypair.first; ++mypair.second;




    1. // predikt taqqoslash yordamida:

    2. mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);

    3. cout << "Ikkinchi mos kelmaydigan elementlar: " << *mypair.first;

    4. cout << " va " << *mypair.second << '\n';

    5. getchar();

    6. return 0;

    7. }

    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:

    1. #include "stdafx.h"

    2. #include // std::cout

    3. #include // std::min_element, std::max_element




    1. bool myfn(int i, int j) { return i




    1. struct myclass {

    2. bool operator() (int i,int j) { return i

    3. } myobj;

    4. using namespace std;

    5. int main () {

    6. int Int_massiv[] = {3,7,2,5,6,4,9};




    1. // standart taqqoslash yordamida:

    2. cout << "Eng kichik element - " << *min_element(Int_massiv,Int_massiv+7) << '\n';

    3. cout << "Eng katta element - " << *max_element(Int_massiv,Int_massiv+7) << '\n';




    1. // myfn funktsiyasidan komp sifatida foydalanish:

    2. cout << "Eng kichik element " << *min_element(Int_massiv,Int_massiv+7,myfn) << '\n';

    3. cout << "Eng katta element " << *max_element(Int_massiv,Int_massiv+7,myfn) << '\n';




    1. // obyekt sifatida myobj-dan komp sifatida foydalanish:

    2. cout << "Eng kichik element " << *min_element(Int_massiv,Int_massiv+7,myobj) << '\n';

    3. cout << "Eng katta element " << *max_element(Int_massiv,Int_massiv+7,myobj) << '\n';

    4. getchar();

    5. return 0;

    6. }

    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:



    1. #include "stdafx.h"

    2. #include // std::cout

    3. #include // std::for_each

    4. #include // std::vector




    1. void myfunction (int i) { // function:

    2. std::cout << ' ' << i;

    3. }




    1. struct myclass { // function object type:

    2. void operator() (int i) {std::cout << ' ' << i;}

    3. } myobject;

    4. using namespace std;

    5. int main () {

    6. vector myvector;

    7. myvector.push_back(10);

    8. myvector.push_back(20);

    9. myvector.push_back(30);




    1. cout << "myvector tarkibi:";

    2. for_each (myvector.begin(), myvector.end(), myfunction);

    3. cout << '\n';

    4. cout << "myvector contains:";

    5. for_each (myvector.begin(), myvector.end(), myobject);

    6. cout << '\n';

    7. getchar();

    8. return 0;

    9. }


    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:



    1. #include "stdafx.h"




    1. #include // std::cout

    2. #include // std::replace

    3. #include // std::vector

    4. using namespace std;

    5. int main () {

    6. int Int_massiv[] = { 10, 20, 30, 30, 20, 10, 10, 20 };

    7. vector myvector (Int_massiv, Int_massiv+8);

    8. // 10 20 30 30 20 10 10 20




    1. replace (myvector.begin(), myvector.end(), 20, 99);

    2. // 10 99 30 30 99 10 10 99




    1. cout << "myvector tarkibi:";

    2. for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)

    3. cout << ' ' << *it;

    4. cout << '\n';

    5. getchar();

    6. return 0;

    7. }


    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:

    1. #include "stdafx.h"

    2. #include // std::cout

    3. #include // std::remove va remove_if

    4. bool IsOdd (int i) { return ((i%2)==1); }

    5. using namespace std;

    6. int main () {

    7. int Int_massiv[]={1,2,3,4,5,6,7,8,9};

    8. // 3 2 3 4 5 6 3 8 9

    9. //Chegaralar:

    10. int* pbegin = Int_massiv; // ^

    11. int* pend = Int_massiv+sizeof(Int_massiv)/sizeof(int); // ^ ^




    1. pend = remove_if (pbegin, pend, IsOdd);

    2. // 2 4 6 8 ? ? ? ? ?

    3. cout << " remove_if dan keyin Range trakibi:";

    4. for (int* p=pbegin; p!=pend; ++p)

    5. cout << ' ' << *p;

    6. cout << '\n';




    1. int Int_massiv_2[] = {3,2,3,4,3,6,7,3,9};

    2. pbegin = Int_massiv_2;

    3. int* pend_2 = Int_massiv_2+sizeof(Int_massiv_2)/sizeof(int);

    4. pend_2 = remove (pbegin, pend_2, 3);

    5. cout << " remove dan keyin Range trakibi:";

    6. for (int* p=pbegin; p!=pend_2; ++p)

    7. cout << ' ' << *p;

    8. cout << '\n';

    9. getchar();

    10. return 0;

    11. }


    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:

    1. #include // std::cout

    2. #include // std::sort

    3. #include // std::vector




    1. bool myfunction (int i,int j) { return (i

    2. }




    1. struct myclass

    2. {

    3. bool operator() (int i,int j) { return (i

    4. } myobject;

    5. using namespace std;

    6. int main ()

    7. {

    8. int Int_massiv[] = {32,71,12,45,26,80,53,33};

    9. vector myvector (Int_massiv, Int_massiv+8);

    10. // 32 71 12 45 26 80 53 33



    // standart taqqoslash (operator <) yordamida:

    1. sort (myvector.begin(), myvector.begin()+4);

    2. //(12 32 45 71)26 80 53 33




    1. // funksiyadan foydalanib

    2. sort (myvector.begin()+4, myvector.end(), myfunction);

    3. // 12 32 45 71(26 33 53 80)




    1. // objectdan foydalib

    2. sort (myvector.begin(), myvector.end(), myobject);

    3. //(12 26 32 33 45 53 71 80)




    1. // elementlarni chop qilish:

    2. cout << "myvector tarkitibi:";

    3. for (vector::iterator it=myvector.begin(); it!=myvector.end(); ++it)

    4. cout << ' ' << *it;

    5. cout << '\n';

    6. getchar();

    7. return 0;

    8. }

    Dastur natijasi:
    myvector tarkitibi: 12 26 32 33 45 53 71 80


  • Download 89,18 Kb.

    Do'stlaringiz bilan baham:
    1   ...   12   13   14   15   16   17   18   19   20




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

    kiriting | ro'yxatdan o'tish
        Bosh sahifa
    юртда тантана
    Боғда битган
    Бугун юртда
    Эшитганлар жилманглар
    Эшитмадим деманглар
    битган бодомлар
    Yangiariq tumani
    qitish marakazi
    Raqamli texnologiyalar
    ilishida muhokamadan
    tasdiqqa tavsiya
    tavsiya etilgan
    iqtisodiyot kafedrasi
    steiermarkischen landesregierung
    asarlaringizni yuboring
    o'zingizning asarlaringizni
    Iltimos faqat
    faqat o'zingizning
    steierm rkischen
    landesregierung fachabteilung
    rkischen landesregierung
    hamshira loyihasi
    loyihasi mavsum
    faolyatining oqibatlari
    asosiy adabiyotlar
    fakulteti ahborot
    ahborot havfsizligi
    havfsizligi kafedrasi
    fanidan bo’yicha
    fakulteti iqtisodiyot
    boshqaruv fakulteti
    chiqarishda boshqaruv
    ishlab chiqarishda
    iqtisodiyot fakultet
    multiservis tarmoqlari
    fanidan asosiy
    Uzbek fanidan
    mavzulari potok
    asosidagi multiservis
    'aliyyil a'ziym
    billahil 'aliyyil
    illaa billahil
    quvvata illaa
    falah' deganida
    Kompyuter savodxonligi
    bo’yicha mustaqil
    'alal falah'
    Hayya 'alal
    'alas soloh
    Hayya 'alas
    mavsum boyicha


    yuklab olish