C++-এ std::array ক্লাস দক্ষ এবং এটি নিজের আকার জানতে পারে।
অ্যারে-এ অপারেশন করার ফাংশনগুলো
size()= অ্যারের আকার রিটার্ন করে অর্থাৎ এলিমেন্টের সংখ্যা।max_size()= অ্যারে ধারণ করতে পারে সর্বোচ্চ এলিমেন্ট সংখ্যা রিটার্ন করে।get(),at(),operator[]= অ্যারে এলিমেন্ট অ্যাক্সেস করতে।front()= অ্যারের প্রথম এলিমেন্ট রিটার্ন করে।back()= অ্যারের শেষ এলিমেন্ট রিটার্ন করে।empty()= অ্যারে খালি হলে true রিটার্ন করে, অন্যথায় false।fill()= পুরো অ্যারে নির্দিষ্ট একটি ভ্যালু দিয়ে ভরা।swap()= একটি অ্যারের এলিমেন্ট অন্য অ্যারের সাথে স왪 করা।
উপরোক্ত সব অপারেশন ইমপ্লিমেন্ট করার উদাহরণ নিচে দেওয়া হলো -
উদাহরণ কোড
#include <iostream>
#include <array>
using namespace std;
int main() {
array<int,4> a = {10, 20, 30, 40};
array<int,4> a1 = {50, 60, 70, 90};
cout << "The size of array is: ";
// size() দিয়ে অ্যারের আকার
cout << a.size() << endl;
// অ্যারে ধারণ করতে পারে সর্বোচ্চ এলিমেন্ট
cout << "Maximum elements array can hold is: ";
cout << a.max_size() << endl;
// at() দিয়ে অ্যারে এলিমেন্ট প্রিন্ট
cout << "The array elements are (using at()): ";
for (int i = 0; i < 4; i++)
cout << a.at(i) << " ";
cout << endl;
// get() দিয়ে অ্যারে এলিমেন্ট প্রিন্ট
cout << "The array elements are (using get()): ";
cout << get<0>(a) << " " << get<1>(a) << " " << endl;
// operator[] দিয়ে অ্যারে এলিমেন্ট প্রিন্ট
cout << "The array elements are (using operator[]): ";
for (int i = 0; i < 4; i++)
cout << a[i] << " ";
cout << endl;
// অ্যারের প্রথম এলিমেন্ট
cout << "First element of array is: ";
cout << a.front() << endl;
// অ্যারের শেষ এলিমেন্ট
cout << "Last element of array is: ";
cout << a.back() << endl;
cout << "The second array elements before swapping are: ";
for (int i = 0; i < 4; i++)
cout << a1[i] << " ";
cout << endl;
// a1-এর ভ্যালু a-র সাথে স왪
a.swap(a1);
// স왪 করার পর প্রথম ও দ্বিতীয় অ্যারে প্রিন্ট
cout << "The first array elements after swapping are: ";
for (int i = 0; i < 4; i++)
cout << a[i] << " ";
cout << endl;
cout << "The second array elements after swapping are: ";
for (int i = 0; i < 4; i++)
cout << a1[i] << " ";
cout << endl;
// খালি কিনা চেক
a1.empty() ? cout << "Array is empty" : cout << "Array is not empty";
cout << endl;
// অ্যারেকে ১ দিয়ে ফিল
a.fill(1);
// ফিল করার পর অ্যারে কনটেন্ট
cout << "Array content after filling operation is: ";
for (int i = 0; i < 4; i++)
cout << a[i] << " ";
return 0;
}আউটপুট
The size of array is: 4 Maximum elements array can hold is: 4 The array elements are (using at()): 10 20 30 40 The array elements are (using get()): 10 20 The array elements are (using operator[]): 10 20 30 40 First element of array is: 10 Last element of array is: 40 The second array elements before swapping are: 50 60 70 90 The first array elements after swapping are: 50 60 70 90 The second array elements after swapping are: 10 20 30 40 Array is not empty Array content after filling operation is: 1 1 1 1