*iter – iterator ko‘rsatadigann elementni olish; - ++iter




Download 1,83 Mb.
bet44/131
Sana13.05.2024
Hajmi1,83 Mb.
#228405
1   ...   40   41   42   43   44   45   46   47   ...   131
Bog'liq
Tiplarni dinamik tarzda

*iter – iterator ko‘rsatadigann elementni olish;

- ++iter - keyingi elementga murojjat qilish uchun iteratorni harakatlantirish

  • --iter - oldingi elementga murojjat qilish uchun iteratorni harakatlantirish. forward_list konteyner iteratorlari dekrement amalini qo‘llab quvvatlamaydi.

  • iter1 == iter2 - ikki iterator teng, agar ular bir xil iteratorni aniqlagan bo‘lsa.

  • iter1 != iter2 ikki iterator teng emas, agar ular bir xil iteratorni aniqlagan bo‘lsa.



#include "stdafx.h"
#include
#include
using namespace std; int main(){ vector myvector;
for (int i = 0; i < 15; i++)
{myvector.push_back(rand() % 100);}
// vector::iterator iter = myvector.begin(); auto iter = myvector.begin(); while(iter!=myvector.end()) {cout << *iter << " | ";
++iter;}
cout << endl; system("pause");
return 0;

5.6-dastur. Iterator amallaridan foydalanish.

Konteynerlar bilan ishlaganda, konteynerdagi elementlarni qo‘shish yoki o‘chirish ushbu konteyner uchun barcha joriy iteratorlarni, shuningdek, uning elementlariga havola va ko‘rsatgichlarni bekor qilishiga olib kelishi mumkin. Iteratorlar nafaqat elementlarni olish, balki ularni o‘zgartirish imkonini beradi.



5.7-dastur. Iterator yordamida konteynerni elementlari qiymatini o‘zgartirish.#include "stdafx.h"
#include #include using namespace std; int main()
{vector myvector;
for (int i = 0; i < 15; i++)
{myvector.push_back(rand() % 10);}
vector::iterator iter = myvector.begin();
//auto iter = myvektor.begin(); while(iter!=myvector.end())
{*iter = (*iter) * (*iter);++iter;}

for(iter = myvector.begin(); iter!=myvector.end(); ++iter)


{cout << *iter << " | ";}
cout << endl; system("pause"); return 0;
}




5.7-dastur. Output

1 | 49 | 16 | 0 | 81 | 16 | 64 | 64 | 4 | 16 | 25 | 25 | 1 | 49 | 1 |

Dasturda while takrorlanish operatori konteynerning elementlarini iteratorga olib, o‘zini o‘ziga ko‘paytirib, yana shu iteratorga yozadi. Shuning uchun ekranga konteynerdagi sonlarning kvadratlari chiqadi.

Download 1,83 Mb.
1   ...   40   41   42   43   44   45   46   47   ...   131




Download 1,83 Mb.

Bosh sahifa
Aloqalar

    Bosh sahifa



*iter – iterator ko‘rsatadigann elementni olish; - ++iter

Download 1,83 Mb.