User Tools

Site Tools


cpp:strings

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cpp:strings [2017/05/08 05:48] – [Μεταβολή του αλφαριθμητικού] gthanoscpp:strings [2019/05/20 08:13] – [Η κλάση String] gthanos
Line 30: Line 30:
 ===== Η κλάση String ===== ===== Η κλάση String =====
  
-Η //standard// βιβλιοθήκη της C++ διαθέτει την κλάση [[http://www.cplusplus.com/reference/string/string/|string]] η οποία έχει το πλεονέκτημα ότι δεν χρειάζεται να σας απασχολεί η δέσμευση της απαραίτητης μνήμης για την αποθήκευση των αλφαριθμητικών.+Η //standard// βιβλιοθήκη της C++ διαθέτει την κλάση [[http://www.cplusplus.com/reference/string/string/|string]]. Οποιοδήποτε αντικείμενο της κλάσης αυτής μπορείτε να το αντιμετωπίζετε σαν ένα δυναμικά μεταβαλλόμενο πίνακα από χαρακτήρες. Η κλάση //string// έχει το πλεονέκτημα ότι δεν χρειάζεται να σας απασχολεί η δέσμευση της απαραίτητης μνήμης για την αποθήκευση των αλφαριθμητικών. 
  
 Δείτε το παρακάτω παράδειγμα χρήσης της κλάσης //string//: Δείτε το παρακάτω παράδειγμα χρήσης της κλάσης //string//:
Line 163: Line 163:
 iterator erase (iterator first, iterator last);</code>  | Διαγράφει μέρος του //string//, μειώνοντας το μήκος του.  | iterator erase (iterator first, iterator last);</code>  | Διαγράφει μέρος του //string//, μειώνοντας το μήκος του.  |
 |  <code cpp>string& replace (size_t pos,  size_t len,  const string& str); |  <code cpp>string& replace (size_t pos,  size_t len,  const string& str);
-string& replace (size_t pos,  size_t len,  const char* s);</code>  | Αντικαθιστά το υφιστάμενο //string// με το νέο //str// ξεκινώντας από την θέση //pos// του //string// και για μήκος //len//  | +string& replace (size_t pos,  size_t len,  const char* s); 
-|  <code cpp></code>   +string& replace (size_t pos,  size_t len,  const string& str, 
-|  <code cpp></code>  |   | +                 size_t subpos, size_t sublen); 
- <code cpp></code>    |+</code>  | Αντικαθιστά το υφιστάμενο //string// με το νέο //str// ξεκινώντας από την θέση //pos// του //string// και για μήκος //len//  | 
 +|  <code cpp>void swap (string& str);</code> Ανταλλάσει αμοιβαία το περιεχόμενο του τρέχοντος //string// με το περιοχόμενο του //string str//.  
 + 
 +<code cpp append.cpp> 
 +#include <iostream> 
 +#include <string> 
 +using namespace std; 
 + 
 +int main () { 
 +  string str="Hello"; 
 +  string str2=" "; 
 +  char[] str3="World"; 
 +  string str4 = "!\n"; 
 + 
 +  str+=str2; 
 +  str.append(str3); 
 +  str.append(str4); 
 +  cout << str; 
 +
 +</code>
  
 <code cpp replace.cpp> <code cpp replace.cpp>
Line 183: Line 202:
   str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)   str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
   str.replace(8,10,"just a");     // "this is just a phrase."     (3)   str.replace(8,10,"just a");     // "this is just a phrase."     (3)
-  str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4) 
-  str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5) 
- 
-  // Using iterators:                                               0123456789*123456789* 
-  str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1) 
-  str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3) 
-  str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4) 
-  str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5) 
-  str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6) 
   cout << str << endl;   cout << str << endl;
 +}
 +</code>
 +
 +<code cpp chicken.cpp>
 +#include <iostream>
 +#include <string>
 +using namespace std;
 +
 +main (){
 +  string chicken ("chicken");
 +  string egg ("egg");
 +  cout << "Before the swap, chicken comes from " << chicken;
 +  cout << " and egg comes from " << egg << endl;
 +
 +  chicken.swap (egg);
 +  cout << "After the swap, chicken comes from " << chicken;
 +  cout << " and egg comes from " << egg << endl;
 } }
 </code> </code>
Line 199: Line 226:
 ==== Σύγκριση, αναζήτηση, εξαγωγή υπο-αλφαριθμητικών ==== ==== Σύγκριση, αναζήτηση, εξαγωγή υπο-αλφαριθμητικών ====
  
-|  <code cpp></code>   +|  <code cpp>const char* c_str() const;</code> Επιστρέφει ένα //C-string// από ένα αντικείμενο της κλάσης //string//.  
-|  <code cpp></code>   +|  <code cpp>const char* data() const;</code> Ανάλογη με την //c_str()// με την διαφορά ότι η σειρά χαρακτήρων που προκύπτει δεν τερματίζεται με τον χαρακτήρα '\0'.  
-|  <code cpp></code>   +|  <code cpp>size_t copy (char* s, size_t len, size_t pos = 0) const;</code> Αντιγράφει ένα υπο-//string// του τρέχοντος //string// στον πίνακα που δείχνει ο δείκτης //s//
-|  <code cpp></code>   +|  <code cpp>size_t find (const string& str, size_t pos = 0) const; 
-|  <code cpp></code>  |   | +size_t find (const char* str, size_t pos = 0) const; 
-|  <code cpp></code>   +size_t find (const char* str, size_t pos, size_t n) const; 
-|  <code cpp></code>   | +size_t find (char c, size_t pos = 0) const; 
-|  <code cpp></code>  |   +</code> Αναζητά στο //string// την πρώτη εμφάνιση του //string str// ή του χαρακτήρα //c// ξεκινώντας την αναζήτηση από την θέση pos.  
-|  <code cpp></code>   |+|  <code cpp> <code cpp>size_t rfind (const string& str, size_t pos = npos) const; 
 +size_t rfind (const char* str, size_t pos = npos) const; 
 +size_t rfind (const char* str, size_t pos, size_t n) const; 
 +size_t rfind (char c, size_t pos = npos) const; 
 +</code> Αναζητά στο //string// την τελευταία εμφάνιση του //string str// ή του χαρακτήρα //c// ξεκινώντας την αναζήτηση από την θέση //pos// και αγνοώντας όλους του χαρακτήρες που βρίσκονται μετά τη θέση //pos//.  
 +|  <code cpp>string substr (size_t pos = 0, size_t len = npos) const;</code> Επιστρέφει ένα νέο αντικείμενο το οποίο αποτελεί υπο-//string// του τρέχοντος αντικειμένου, ξεκινώντας από την θέση pos και με μήκος len.  | 
 +|  <code cpp>int compare (const string& str) const; 
 +int compare (size_t pos, size_t len, const string& str) const; 
 +int compare (const char* str) const; 
 +int compare (size_t pos, size_t len, const char* str) const;</code> Συγκρίνει το τρέχον string με το δοθέν //string// ή με το υπο-//string// που ξεκινάει από την θέση //pos// του //str// και έχει μήκος //len//.  |
  
 +<code cpp cstring.cpp>
 +#include <iostream>
 +#include <cstring>
 +#include <string>
 +using namespace std;
  
 +int main () {
 +  string str ("Split this sentence to word tokens");
 +  char * cstr = new char [str.length()+1];
 +  strcpy (cstr, str.c_str());
 +
 +  char * p = strtok (cstr," ");
 +  while (p!=0) {
 +    cout << p << '\n';
 +    p = strtok(NULL," ");
 +  }
 +  delete[] cstr;
 +}
 +</code>
cpp/strings.txt · Last modified: 2021/04/27 04:45 (external edit)