অ্যারে হল C++-এ একটি ডেটা স্ট্রাকচার যা একই ডেটা টাইপের একাধিক ডেটা এলিমেন্ট কনটিনিউয়াস মেমরি লোকেশনে সংরক্ষণ করে।
C++ প্রোগ্রামিং ভাষায় অ্যারে টাইপ ম্যানিপুলেট করার জন্য ইনবিল্ট ফাংশন রয়েছে। কিছু ফাংশন মাল্টিডাইমেনশনাল অ্যারেতেও প্রয়োগ করা যায়। অ্যারে হেডার ফাইলে অ্যারে ম্যানিপুলেট করার ফাংশনগুলো থাকে।
C++-এ অ্যারে ম্যানিপুলেট করার কিছু সাধারণ মেথড:
is_array()
এই ফাংশনটি ব্যবহার করা হয় ফাংশনে পাস করা ভেরিয়েবলটি অ্যারে টাইপের কিনা তা চেক করতে। এটি অ্যারে রিকগনাইজ করার বিষয়ে কঠোর, এমনকি std::array কেও রিজেক্ট করে দেয়। রিটার্ন টাইপ ইন্টিজার অর্থাৎ অ্যারে পাস করলে true (1) রিটার্ন করে, অন্যথায় false (0)।
উদাহরণ
#include <type_traits>
#include <iostream>
#include <array>
#include <string>
using namespace std;
int main() {
cout << "Checking if int is an array ? : ";
is_array<int>::value ? cout << "True" : cout << "False";
cout << "\nChecking if int[] is an array? : ";
is_array<int[6]>::value ? cout << "True" : cout << "False";
cout << "\nChecking if 2D Array is an array? : ";
is_array<int[2][3]>::value ? cout << "True" : cout << "False";
cout << "\nChecking if String is an array? : ";
is_array<string>::value ? cout << "True" : cout << "False";
cout << "\nChecking if Character Array is an array? : ";
is_array<char[4]>::value ? cout << "True" : cout << "False";
cout << endl;
return 0;
}
আউটপুট
Checking if int is an array ? : False Checking if int[] is an array? : True Checking if 2D Array is an array? : True Checking if String is an array? : False Checking if Character Array is an array? : True
is_same()
এই ফাংশনটি ব্যবহার করা হয় দুটি টাইপ একই ব্লুপ্রিন্টের কিনা অর্থাৎ দুটির টাইপ একই কিনা তা চেক করতে।
উদাহরণ
#include <type_traits>
#include <iostream>
#include <array>
#include <string>
using namespace std;
int main() {
cout << "Checking if 1D array is same as 1D array (Different sizes) ? : ";
is_same<int[3], int[4]>::value ? cout << "True" : cout << "False";
cout << "\nChecking if 1D array is same as 1D array? (Same sizes): ";
is_same<int[5], int[5]>::value ? cout << "True" : cout << "False";
cout << "\nChecking If 2D array is same as 1D array? : ";
is_same<int[3], int[3][4]>::value ? cout << "True" : cout << "False";
cout << "\nChecking if Character array is same as Integer array? : ";
is_same<int[5], char[5]>::value ? cout << "True" : cout << "False";
return 0;
}
আউটপুট
Checking if 1D array is same as 1D array (Different sizes) ? : False Checking if 1D array is same as 1D array? (Same sizes): True Checking If 2D array is same as 1D array? : False Checking if Character array is same as Integer array? : False
rank()
rank ফাংশনটি পাস করা অ্যারে র্যাঙ্ক রিটার্ন করে। Rank মানে অ্যারের ডাইমেনশন। এটি অ্যারের র্যাঙ্কের ইন্টিজার ভ্যালু রিটার্ন করে।
উদাহরণ
#include <type_traits>
#include <iostream>
using namespace std;
int main() {
cout << "Print rank for the following types of arrays : \n";
cout << "integer : " << rank<int>::value << endl;
cout << "1D integer array (int[]) : " << rank<int[5]>::value << endl;
cout << "2D integer array (int[][]) : " << rank<int[2][2]>::value << endl;
cout << "3D integer array (int[][][]) : " << rank<int[2][3][4]>::value << endl;
cout << "1D character array : " << rank<char[10]>::value << endl;
}
আউটপুট
Print rank for the following types of arrays : integer : 0 1D integer array (int[]) : 1 2D integer array (int[][]) : 2 3D integer array (int[][][]) : 3 1D character array : 1
extent()
C++-এ extent() মেথড একটি অ্যারের ডাইমেনশনের আকার রিটার্ন করে। এই মেথডের দুটি ইনপুট প্যারামিটার রয়েছে: অ্যারে এবং ডাইমেনশন।
উদাহরণ
#include <type_traits>
#include <iostream>
using namespace std;
int main() {
cout << "Printing the length of all dimensions of the array arr[2][45][5] :\n";
cout << "1st dimension : " << extent<int[2][45][5], 0>::value << endl;
cout << "2nd dimension : " << extent<int[2][45][5], 1>::value << endl;
cout << "3rd dimension : " << extent<int[2][45][5], 2>::value << endl;
cout << "4th dimension : " << extent<int[2][45][5], 3>::value << endl;
}
আউটপুট
Printing the length of all dimensions of the array arr[2][45][5] : 1st dimension : 2 2nd dimension : 45 3rd dimension : 5 4th dimension : 0
remove_extent()
remove_extent ফাংশনটি ব্যবহার করা হয় মাল্টিডাইমেনশনাল অ্যারের ডাইমেনশন মুছে ফেলতে। এটি অ্যারের প্রথম ডাইমেনশন মুছে ফেলে।
উদাহরণ
#include <type_traits>
#include <iostream>
using namespace std;
int main() {
cout << "Removing extent of the array arr[2][5][4] : \n";
cout << "Initial rank : " << rank<int[2][5][4]>::value << endl;
cout << "The rank after removing 1 extent is : ";
cout << rank<remove_extent<int[20][10][30]>::type>::value << endl;
cout << "length of 1st dimension after removal is :";
cout << extent<remove_extent<int[20][10][30]>::type>::value << endl;
}
আউটপুট
Removing extent of the array arr[2][5][4] : Initial rank : 3 The rank after removing 1 extent is : 2 length of 1st dimension after removal is : 10
remove_all_extents()
এই ফাংশনটি একসাথে অ্যারের সব ডাইমেনশন মুছে ফেলতে ব্যবহৃত হয়। অ্যারেটি একই টাইপের ভেরিয়েবলে রূপান্তরিত হয়।
উদাহরণ
#include <type_traits>
#include <iostream>
using namespace std;
int main() {
cout << "Removing all extents of the array arr[2][5][4] : \n";
cout << "Initial rank : " << rank<int[2][5][4]>::value << endl;
cout << "The rank after removing all extents is : ";
cout << rank<remove_all_extents<int[20][10][30]>::type>::value << endl;
cout << "length of 1st dimension after removal is :";
cout << extent<remove_all_extents<int[20][10][30]>::type>::value << endl;
}
আউটপুট
Removing all extents of the array arr[2][5][4] : Initial rank : 3 The rank after removing all extents is : 0 length of 1st dimension after removal is : 0