يحتوي C++ في تعريفه على طريقة لتمثيل ملف تسلسل الأحرف ككائن للفئة . تسمى هذه الفئة std:: string. تقوم فئة السلسلة بتخزين الأحرف كسلسلة من البايتات مع وظيفة السماح الوصول إلى حرف البايت الواحد .
سلسلة مقابل صفيف الأحرف
خيط | مصفوفة شار |
|---|---|
| السلسلة هي أ الطبقة التي تحدد الكائنات التي يتم تمثيلها كتيار من الشخصيات. | مجموعة الأحرف هي ببساطة مجموعة من الشخصيات التي يمكن إنهاؤها بحرف فارغ. |
| في حالة سلاسل الذاكرة المخصصة ديناميكيا . يمكن تخصيص المزيد من الذاكرة في وقت التشغيل عند الطلب. نظرًا لعدم تخصيص أي ذاكرة مسبقًا لا تضيع الذاكرة . | يجب أن يكون حجم مصفوفة الأحرف المخصصة بشكل ثابت لا يمكن تخصيص المزيد من الذاكرة في وقت التشغيل إذا لزم الأمر. المخصصة غير المستخدمة وتضيع الذاكرة أيضا |
| كما يتم تمثيل السلاسل ككائنات لا تسوس مجموعة يحدث. | هناك تهديد اضمحلال المصفوفة في حالة مجموعة الأحرف. |
| السلاسل أبطأ بالمقارنة مع التنفيذ من مجموعة الأحرف. | تنفيذ مصفوفة الأحرف أسرع من الأمراض المنقولة جنسيا:: سلسلة. |
| تحدد فئة السلسلة عدد من الوظائف التي تسمح بعمليات متعددة على السلاسل. | مصفوفات الأحرف لا تقدم كثير وظائف يحمل في ثناياه عوامل للتلاعب بالسلاسل. |
العمليات على السلاسل
1) وظائف الإدخال
| وظيفة | تعريف |
|---|---|
| خط الحصول () | تُستخدم هذه الوظيفة لتخزين دفق من الأحرف التي أدخلها المستخدم في ذاكرة الكائن. |
| Push_back() | تُستخدم هذه الوظيفة لإدخال حرف في نهاية السلسلة. |
| pop_back() | تم تقديم هذه الوظيفة من C++ 11 (للسلاسل) لحذف الحرف الأخير من السلسلة. |
مثال:
CPP// C++ Program to demonstrate the working of // getline() push_back() and pop_back() #include #include // for string class using namespace std; // Driver Code int main() { // Declaring string string str; // Taking string input using getline() getline(cin str); // Displaying string cout << 'The initial string is : '; cout << str << endl; // Inserting a character str.push_back('s'); // Displaying string cout << 'The string after push_back operation is : '; cout << str << endl; // Deleting a character str.pop_back(); // Displaying string cout << 'The string after pop_back operation is : '; cout << str << endl; return 0; }
الإخراج
The initial string is : The string after push_back operation is : s The string after pop_back operation is :
تعقيد الوقت: O(1)
تعقيد الفضاء: O(n) حيث n هو حجم السلسلة
كيفية قراءة ملف json
2) وظائف القدرات
| وظيفة | تعريف |
|---|---|
| سعة() | تقوم هذه الدالة بإرجاع السعة المخصصة للسلسلة والتي يمكن أن تساوي حجم السلسلة أو تزيد عنها. يتم تخصيص مساحة إضافية بحيث يمكن إجراء العمليات بكفاءة عند إضافة الأحرف الجديدة إلى السلسلة. |
| تغيير الحجم () | تعمل هذه الوظيفة على تغيير حجم السلسلة ويمكن زيادة الحجم أو تقليله. |
| طول() | تبحث هذه الوظيفة عن طول السلسلة. |
| تعمل هذه الوظيفة على تقليل سعة السلسلة وتجعلها مساوية للحد الأدنى لسعة السلسلة. تعتبر هذه العملية مفيدة لحفظ ذاكرة إضافية إذا كنا متأكدين من عدم الحاجة إلى إضافة أحرف أخرى. |
مثال:
CPP// C++ Program to demonstrate the working of // capacity() resize() and shrink_to_fit() #include #include // for string class using namespace std; // Driver Code int main() { // Initializing string string str = 'geeksforgeeks is for geeks'; // Displaying string cout << 'The initial string is : '; cout << str << endl; // Resizing string using resize() str.resize(13); // Displaying string cout << 'The string after resize operation is : '; cout << str << endl; // Displaying capacity of string cout << 'The capacity of string is : '; cout << str.capacity() << endl; // Displaying length of the string cout << 'The length of the string is :' << str.length() << endl; // Decreasing the capacity of string // using shrink_to_fit() str.shrink_to_fit(); // Displaying string cout << 'The new capacity after shrinking is : '; cout << str.capacity() << endl; return 0; }
الإخراج
The initial string is : geeksforgeeks is for geeks The string after resize operation is : geeksforgeeks The capacity of string is : 26 The length of the string is :13 The new capacity after shrinking is : 13
تعقيد الوقت: O(1)
تعقيد الفضاء: O(n) حيث n هو حجم السلسلة
3) وظائف التكرار
| وظيفة | تعريف |
|---|---|
| يبدأ() | تقوم هذه الدالة بإرجاع مكرر إلى بداية السلسلة. |
| نهاية() | تقوم هذه الوظيفة بإرجاع مكرر إلى التالي لنهاية السلسلة. |
| ربجين () | تقوم هذه الدالة بإرجاع مكرر عكسي يشير إلى نهاية السلسلة. |
| يجعل() | تقوم هذه الدالة بإرجاع مكرر عكسي يشير إلى بداية السلسلة السابقة. |
| كبداية () | ترجع هذه الدالة مكررًا ثابتًا يشير إلى بداية السلسلة ولا يمكن استخدامه لتعديل المحتويات التي تشير إليها. |
| عدد قليل () | ترجع هذه الدالة مكررًا ثابتًا يشير إلى نهاية السلسلة التالية، ولا يمكن استخدامه لتعديل المحتويات التي تشير إليها. |
| كربيجين () | تُرجع هذه الدالة مكررًا عكسيًا ثابتًا يشير إلى نهاية السلسلة ولا يمكن استخدامه لتعديل المحتويات التي تشير إليها. |
| كريند () | تُرجع هذه الدالة مكررًا عكسيًا ثابتًا يشير إلى بداية السلسلة السابقة ولا يمكن استخدامه لتعديل المحتويات التي تشير إليها. |
الخوارزمية:
- أعلن سلسلة
- حاول تكرار السلسلة باستخدام جميع أنواع التكرارات
- حاول تعديل عنصر السلسلة.
- عرض كافة التكرارات.
مثال:
CPP// C++ Program to demonstrate the working of // begin() end() rbegin() rend() cbegin() cend() crbegin() crend() #include #include // for string class using namespace std; // Driver Code int main() { // Initializing string` string str = 'geeksforgeeks'; // Declaring iterator std::string::iterator it; // Declaring reverse iterator std::string::reverse_iterator it1; cout<<'Str:'<<str<<'n'; // Displaying string cout << 'The string using forward iterators is : '; for (it = str.begin(); it != str.end(); it++){ if(it == str.begin()) *it='G'; cout << *it; } cout << endl; str = 'geeksforgeeks'; // Displaying reverse string cout << 'The reverse string using reverse iterators is ' ': '; for (it1 = str.rbegin(); it1 != str.rend(); it1++){ if(it1 == str.rbegin()) *it1='S'; cout << *it1; } cout << endl; str = 'geeksforgeeks'; //Displaying String cout<<'The string using constant forward iterator is :'; for(auto it2 = str.cbegin(); it2!=str.cend(); it2++){ //if(it2 == str.cbegin()) *it2='G'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content but we can inc/dec-rement the iterator cout<<*it2; } cout<<'n'; str = 'geeksforgeeks'; //Displaying String in reverse cout<<'The reverse string using constant reverse iterator is :'; for(auto it3 = str.crbegin(); it3!=str.crend(); it3++){ //if(it2 == str.cbegin()) *it2='S'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content but we can inc/dec-rement the iterator cout<<*it3; } cout<<'n'; return 0; } //Code modified by Balakrishnan R (rbkraj000)
الإخراج
Str:geeksforgeeks The string using forward iterators is : Geeksforgeeks The reverse string using reverse iterators is : Skeegrofskeeg The string using constant forward iterator is :geeksforgeeks The reverse string using constant reverse iterator is :skeegrofskeeg
تعقيد الوقت: O(1)
تعقيد الفضاء: O(n) حيث n هو حجم السلسلة
4) وظائف التلاعب:
| وظيفة | تعريف |
|---|---|
| نسخ ("مصفوفة شار" لين بوس) | تقوم هذه الوظيفة بنسخ السلسلة الفرعية في مصفوفة الأحرف المستهدفة المذكورة في وسيطاتها. يستغرق نسخ طول مصفوفة الأحرف المستهدفة 3 وسيطات وموضع البداية في السلسلة لبدء النسخ. |
| تبديل() | تقوم هذه الوظيفة بتبديل سلسلة واحدة بأخرى |
مثال:
CPP// C++ Program to demonstrate the working of // copy() and swap() #include #include // for string class using namespace std; // Driver Code int main() { // Initializing 1st string string str1 = 'geeksforgeeks is for geeks'; // Declaring 2nd string string str2 = 'geeksforgeeks rocks'; // Declaring character array char ch[80]; // using copy() to copy elements into char array // copies 'geeksforgeeks' str1.copy(ch 13 0); // Displaying char array cout << 'The new copied character array is : '; cout << ch << endl; // Displaying strings before swapping cout << 'The 1st string before swapping is : '; cout << str1 << endl; cout << 'The 2nd string before swapping is : '; cout << str2 << endl; // using swap() to swap string content str1.swap(str2); // Displaying strings after swapping cout << 'The 1st string after swapping is : '; cout << str1 << endl; cout << 'The 2nd string after swapping is : '; cout << str2 << endl; return 0; }
الإخراج
The new copied character array is : geeksforgeeks The 1st string before swapping is : geeksforgeeks is for geeks The 2nd string before swapping is : geeksforgeeks rocks The 1st string after swapping is : geeksforgeeks rocks The 2nd string after swapping is : geeksforgeeks is for geeks
يجب أن تقرأ: فئة السلسلة C++ وتطبيقاتها