مرحبًا، يا شباب! لذلك، كمبرمجين، نتعامل غالبًا مع مصفوفات من جميع أنواع البيانات. سنغطي مصفوفة سلاسل C++ في مقال اليوم.
طرق تعريف مصفوفة سلاسل C++

١. الكلمة الرئيسية String لإنشاء مصفوفة سلاسل في C++
C++ provides us with ‘string’ keyword to declare and manipulate data in a String array.
الكلمة الرئيسية string
تخصص ذاكرة لعناصر المصفوفة في الذاكرة بناءً على ديناميكية أو وقت التشغيل على التوالي. وبالتالي، يُخفف من عبء تخصيص الذاكرة الثابتة لعناصر البيانات.
بناء الجملة: لتعريف مصفوفة سلاسل باستخدام الكلمة الرئيسية ‘string’
string array-name[size];
علاوة على ذلك، يمكننا تهيئة مصفوفة السلاسل باستخدام بناء الجملة التالي:
string array-name[size]={'val1','val2',.....,'valN'};
مثال ١:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string fruits[5] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" };
cout<<"String array:\n";
for (int x = 0; x< 5; x++)
cout << fruits[x] << "\n";
}
في المثال أعلاه، قمنا بتهيئة مصفوفة السلاسل واستخدمنا حلقات C++ لتمرير المصفوفة وطباعة عناصر البيانات الموجودة في مصفوفة السلاسل.
الإخراج:
String array:
Grapes
Apple
Pineapple
Banana
Jackfruit
مثال ٢:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string arr[5];
cout<<"Enter the elements:"<<endl;
for(int x = 0; x<5;x++)
{
cin>>arr[x];
}
cout<<"\nString array:\n";
for (int x = 0; x< 5; x++)
cout << arr[x] << "\n";
}
كما يمكن للجميع أن يلاحظ، في المثال أعلاه، قمنا بقبول عناصر بيانات مصفوفة السلاسل من وحدة التحكم أي أخذ إدخال المستخدم وقمنا بطباعة عناصر مصفوفة السلاسل.
الإخراج:
Enter the elements:
Jim
Nick
Daisy
Joha
Sam
String array:
Jim
Nick
Daisy
Joha
Sam
2. استخدام حاوية C++ STL – Vector
C++ Standard Template Library provides us with containers to work with data and store it efficiently.
الـ Vector، كونها واحدة من هذه الحاويات، تقوم بتخزين عناصر المصفوفة بطريقة ديناميكية. وبالتالي، يمكن استخدام الـ Vectors في C++ لإنشاء مصفوفة سلسلة والتعامل معها بسهولة.
الصيغة:
vector<string>array-name;
- يُستخدم الأمر
vector.push_back(element)
لإضافة العناصر إلى مصفوفة السلسلة. - يُستخدم الأمر
vector.size()
لحساب طول المصفوفة أي عدد العناصر المدخلة في مصفوفة السلسلة.
مثال:
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<string> arr;
arr.push_back("Ace");
arr.push_back("King");
arr.push_back("Queen");
int size = arr.size();
cout<<"Elements of the vector array:"<<endl;
for (int x= 0; x< size; x++)
cout << arr[x] << "\n";
}
الإخراج:
Elements of the vector array:
Ace
King
Queen
3. باستخدام مصفوفة حرفيّة ثنائية الأبعاد
A 2D array represents an array of string in C++. So, we can use a 2D char array to represent string type elements in an array.
تقوم المصفوفة الحرفية بإنشاء وتخزين العناصر في وقت التشغيل أو في وقت الترجمة أي أن عدد العناصر وحجمها يبقى ثابتًا/ثابتًا.
الصيغة:
char array-name[number-of-items][maximun_size-of-string];
مثال:
#include <bits/stdc++.h>
using namespace std;
int main()
{
char fruits[5][10] = { "Grapes", "Apple","Pineapple", "Banana", "Jackfruit" };
cout<<"Character array:\n";
for (int x = 0; x< 5; x++)
cout << fruits[x] << "\n";
}
في مقطع الكود أعلاه، قمنا بإنشاء مصفوفة من نوع char لتخزين عناصر من نوع string، أي مصفوفة char[5][10]. هنا، يُظهر الرقم 5 عدد عناصر السلسلة والرقم 10 يشير إلى الحجم الأقصى لسلسلة الإدخال.
الناتج:
Character array:
Grapes
Apple
Pineapple
Banana
Jackfruit
C++ String Array as an Argument to a Function
A string array can also be passed to a function as an argument the same way as another non-string type array is passed to it.
الصياغة:
return-type function-name(string array-name[size])
{
// جسم الدالة
}
مثال:
#include <iostream>
#include<string>
using namespace std;
void show(string arr[4]){
for(int x=0;x<4;x++)
{
cout<<arr[x]<<endl;
}
}
int main() {
string arr[4] = {"Jim", "Jeo", "Jio", "John"};
cout<<"Printing elements of the string array:"<<endl;
show(arr);
}
الناتج:
Printing elements of the string array:
Jim
Jeo
Jio
John
الاستنتاج
في هذا المقال، فهمنا طرق إنشاء مصفوفات سلسلة وتقنيات استخدامها في دالة.
المراجع
Source:
https://www.digitalocean.com/community/tutorials/string-array-in-c-plus-plus