5. Shift (): —Ushbu funksiya elementlarni argument sifatida ko'rsatilgan raqamga o'tkazgandan so'ng, yangi valarray ni qaytaradi. Agar raqam musbat bo'lsa, chap siljish qo'llaniladi, agar salbiy bo'lsa, o'ng tomonga siljish qo'llaniladi.
6. CSHIFT (): — Ushbu funktsiya argument sifatida ko'rsatilgan raqam bo'yicha elementlarni aylantirgan (aylantirgan) dan keyin yangi valarrayni qaytaradi. Agar raqam musbat bo'lsa, chap siljish va aylana qo'llaniladi, agar son salbiy bo'lsa, chap siljish qo'llaniladi.
// Ishni namoyish qilish uchun C ++ kodi
// shift () и cshift ()
#include
#include // for valarray functions
using namespace std;
int main()
{
// valarray ni ishga tushurish
valarray varr = { 10, 2, 20, 1, 30 };
// yangi valarrayni e’lon qilish
valarray varr1;
// elementlarni chapga siljitish uchun shift () foydalaniladi
// valarray ni 2ta o’ringa surish
varr1 = varr.shift(2);
//elementlar surilgan keyin valarray ko’rinishi
cout << "The new valarray after shifting is : ";
for ( int&x : varr1) cout << x << " ";
cout << endl;
// elementlarni o'ngga aylantirish uchun cshift () dan foydalaning
// 3 ta o'ringa siljitish
varr1 = varr.cshift(-3);
// Dumaloq aylanishdan keyin valarray elementlarini ko’rish
cout << "The new valarray after circular shifting is : ";
for ( int&x : varr1) cout << x << " ";
cout << endl;
return 0;
}
|
Выход:
The new valarray after shifting is : 20 1 30 0 0
The new valarray after circular shifting is : 20 1 30 10 2
7. swap () : — Bu funksiya bir valarray ni boshqasiga o’zgartiradi.
// C ++ da swap() ni ishlatish uchun masala
#include
#include // for valarray functions
using namespace std;
int main()
{
// 1- valarray ishga tushushi
valarray varr1 = {1, 2, 3, 4};
// 2- valarray ishga tushushi
valarray varr2 = {2, 4, 6, 8};
// O’zgarishdan oldingi qiymatlarni ko’rish
cout << "The contents of 1st valarray "
"before swapping are : ";
for (int &x : varr1)
cout << x << " ";
cout << endl;
cout << x << " ";
cout << "The contents of 2nd valarray "
"before swapping are : ";
for (int &x : varr2)
cout << endl;
//Qiymatlarni almashtirish uchun swap () dan foydalanish
varr1.swap(varr2);
// Qiymatlarni almashishdan keying holati
cout << "The contents of 1st valarray "
"after swapping are : ";
for (int &x : varr1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd valarray "
"after swapping are : ";
for (int &x : varr2)
cout << x << " ";
cout << endl;
return 0;
}
|
Выход:
The contents of 1st valarray before swapping are : 1 2 3 4
The contents of 2nd valarray before swapping are : 2 4 6 8
The contents of 1st valarray after swapping are : 2 4 6 8
The contents of 2nd valarray after swapping are : 1 2 3 4
Do'stlaringiz bilan baham: |