std::string class ב-C++

ל-C++ יש בהגדרה שלו דרך לייצג את a רצף של תווים כאובייקט של המחלקה . מחלקה זו נקראת std:: string. מחלקת המחרוזת מאחסנת את התווים כרצף של בתים עם הפונקציונליות של התרת גישה לתו של בייט בודד .

מחרוזת מול מערך תווים

חוּט

מערך Char

 מחרוזת היא א מחלקה המגדירה אובייקטים שיוצגו כזרם של דמויות. מערך תווים הוא פשוט an מערך דמויות שניתן לסיים באמצעות תו ריק.
במקרה של זיכרון מחרוזות הוא מוקצה באופן דינמי . ניתן להקצות יותר זיכרון בזמן ריצה לפי דרישה. מכיוון ששום זיכרון לא מוקצה מראש שום זיכרון לא מתבזבז . הגודל של מערך התווים חייב להיות מוקצה באופן סטטי לא ניתן להקצות יותר זיכרון בזמן ריצה במידת הצורך. לא בשימוש מוקצה גם הזיכרון מתבזבז
כמחרוזות מיוצגות כאובייקטים ללא דעיכה של מערך מתרחשת. יש א איום של ריקבון מערך במקרה של מערך התווים. 
המיתרים איטיים יותר בהשוואה ליישום מאשר מערך תווים. יישום של מערך התווים מהיר יותר מאשר std:: מחרוזת. 
מחלקה מחרוזת מגדירה מספר פונקציות המאפשרים פעולות רבות על מיתרים. מערכי תווים לא מציעים רַבִּים לתפעל מיתרים.

פעולות על מיתרים

1) פונקציות קלט

פוּנקצִיָה הַגדָרָה
getline() פונקציה זו משמשת לאחסון זרם תווים כפי שהוזן על ידי המשתמש בזיכרון האובייקט.
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 הוא גודל המחרוזת

2) פונקציות קיבולת

פוּנקצִיָה הַגדָרָה
יְכוֹלֶת() פונקציה זו מחזירה את הקיבולת שהוקצתה למחרוזת שיכולה להיות שווה או יותר מגודלה של המחרוזת. מקום נוסף מוקצה כך שכאשר התווים החדשים מתווספים למחרוזת ניתן לבצע את הפעולות ביעילות.
שינוי גודל () פונקציה זו משנה את גודל המחרוזת, ניתן להגדיל או להקטין את הגודל.
מֶשֶׁך() פונקציה זו מוצאת את אורך המחרוזת.
shrink_to_fit() פונקציה זו מקטינה את הקיבולת של המיתר והופכת אותה לשווה לקיבולת המינימלית של המיתר. פעולה זו שימושית כדי לחסוך זיכרון נוסף אם אנו בטוחים שאין צורך להוסיף עוד תווים.

דוּגמָה:

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) פונקציות איטרטור

פוּנקצִיָה הַגדָרָה
לְהַתְחִיל() פונקציה זו מחזירה איטרטור לתחילת המחרוזת.
סוֹף() פונקציה זו מחזירה איטרטור לקצה הבא של המחרוזת.
rbegin() פונקציה זו מחזירה איטרטור הפוך המצביע על קצה המחרוזת.
לְדַקלֵם() פונקציה זו מחזירה איטרטור הפוך המצביע על ההתחלה של המחרוזת הקודמת.
cbegin() פונקציה זו מחזירה איטרטור קבוע המצביע על תחילת המחרוזת שלא ניתן להשתמש בה כדי לשנות את התוכן שאליו הוא מצביע.
אֲחָדִים() פונקציה זו מחזירה איטרטור קבוע המצביע על הקצה הבא של המחרוזת, לא ניתן להשתמש בה כדי לשנות את התוכן שאליו הוא מצביע.
crbegin() פונקציה זו מחזירה איטרטור הפוך קבוע המצביע על קצה המחרוזת שלא ניתן להשתמש בה כדי לשנות את התוכן שאליו הוא מצביע.
crend() פונקציה זו מחזירה איטרטור הפוך קבוע המצביע על ההתחלה או הקודמת של המחרוזת. לא ניתן להשתמש בה כדי לשנות את התוכן שאליו הוא מצביע.

אַלגוֹרִיתְם:

  1. הכריז על מחרוזת
  2. נסה לחזור על המחרוזת באמצעות כל סוגי האיטרטורים
  3. נסה לשנות את האלמנט של המחרוזת.
  4. הצג את כל האיטרציות.

דוּגמָה:

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) מניפולציה של פונקציות:

פוּנקצִיָה הַגדָרָה
copy('מערך char' len pos)  פונקציה זו מעתיקה את המחרוזת המשנה במערך תווי היעד המוזכר בארגומנטים שלה. נדרשים 3 ארגומנטים של אורך מערך ה-char כדי להעתיק אותם ומיקום ההתחלה במחרוזת כדי להתחיל להעתיק.
לְהַחלִיף() פונקציה זו מחליפה מחרוזת אחת באחרת

דוּגמָה:

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++ והיישומים שלה