Tiplarni dinamik tarzda




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

O‘zgarmas iteratorlar. Agar konteynerda o‘zgarmas qiymatli elementlar bo‘lsa, bu holda konteyner elementlariga murojaat qilish uchun o‘zgarmas iteratorlardan foydalanishsh kerak. Buning uchun const_iteratop tipi ishlatiladi. Bu iteratorlar faqat elementlarni sanash imkonini beradi. O‘zgartirish mumkin emas.

vector::const_iterator iter ;
for(iter = myvector.begin(); iter!=myvector.end(); ++iter)
{cout << *iter << " | ";
//*iter = (*iter) * (*iter);
}

O‘zgarmas iteratorning qiymatlarini olish uchun cbegin() va cend() funksiyalari ham ishlatiladi. Agar iterator o‘zgarmas deb olinmagan bo‘lsa ham, bu funksiyalar uni o‘zgarmas qilib beradi. Shungdek, o‘zgartirish mumkin emas bo‘ladi.
5.8-dastur. O‘zgarmas iteratordan foydalanish.


// ConsoleApplication1.cpp : main project file.

#include "stdafx.h" #include #include using namespace std;


int main(){


int myints[] = {16,2,77,29,28};
const vector myvector (myints, myints + sizeof(myints) / sizeof(myints[0])
); vector myvector_one; for (int i = 0; i < 15; i++)
myvector_one.push_back(rand() % 10);
vector::const_iterator it;



for (it = myvector.begin(); it != myvector.end(); ++it) cout << ' ' << *it;
cout << endl;
for (it = myvector_one.cbegin(); it != myvector_one.cend(); ++it) cout << ' ' << *it;
cout << endl; system("pause"); return 0;
}

5.8-dastur.Output

16 2 77 29 28
1 7 4 0 9 4 8 8 2 4 5 5 1 7 1



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




Download 1,83 Mb.