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:20] – [Πρόσβαση σε χαρακτήρες του αλφαριθμητικού] 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 110: Line 110:
  
 |  <code cpp>iterator begin(); |  <code cpp>iterator begin();
-const_iterator begin() const;</code>  | Επιστρέφει έναν //kiterator// που δείχνει στον πρώτο χαρακτήρα του //string//+const_iterator begin() const;</code>  | Επιστρέφει έναν //iterator// που δείχνει στον πρώτο χαρακτήρα του //string//
-|  <code cpp></code>  |   | +|  <code cpp>iterator end(); 
-|  <code cpp></code>   | +const_iterator end() const;</code> Επιστρέφει έναν //iterator// που δείχνει μετά τον τελευταίο χαρακτήρα του //string// | 
-|  <code cpp></code>  |   | +|  <code cpp>reverse_iterator rbegin(); 
-|  <code cpp></code>  |   | +const_reverse_iterator rbegin() const;</code> Επιστρέφει έναν //reverse iterator// που δείχνει στον τελευταίο χαρακτήρα του //string// | 
-|  <code cpp></code>  |   | +|  <code cpp>reverse_iterator rend(); 
-|  <code cpp></code>  |   | +const_reverse_iterator rend() const;</code> Επιστρέφει έναν //reverse iterator// που δείχνει πριν από τον πρώτο χαρακτήρα του //string//  |
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>   | +
-|  <code cpp></code>  |   +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   |+
  
 === Παράδειγμα χρήσης iterator === === Παράδειγμα χρήσης iterator ===
  
 <code cpp> <code cpp>
-// string::begin/end 
 #include <iostream> #include <iostream>
 #include <string> #include <string>
Line 141: Line 133:
 </code> </code>
  
 +=== Παράδειγμα χρήσης reverse_iterator ===
  
 +<code cpp>
 +#include <iostream>
 +#include <string>
 +using namespace std;
 +
 +int main (){
 +  string str = "Hello World!";
 +  for (string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
 +    cout << *rit;
 +  cout << endl;
 +}
 +</code>
  
 ==== Μεταβολή του αλφαριθμητικού ==== ==== Μεταβολή του αλφαριθμητικού ====
Line 148: Line 153:
 string& operator+= (const char* s); string& operator+= (const char* s);
 string& operator+= (char c);</code>  | Επεκτείνει το αλφαριθμητικό προσθέτοντας τους επιπλέον χαρακτήρες του ορίσματος στο τέλος του. Επιστρέφει μία αναφορά στο τρέχον αντικείμενο. | string& operator+= (char c);</code>  | Επεκτείνει το αλφαριθμητικό προσθέτοντας τους επιπλέον χαρακτήρες του ορίσματος στο τέλος του. Επιστρέφει μία αναφορά στο τρέχον αντικείμενο. |
-|  <code cpp></code>   +|  <code cpp>string& append (const string& str); 
-|  <code cpp></code>   +string& append (const char* str);</code> Επεκτείνει το αλφαριθμητικό ενθέτοντας ένα αντίγραφο του //str// στο τέλος του.  
-|  <code cpp></code>   +|  <code cpp>string& assign (const string& str); 
-|  <code cpp></code>   +string& assign (const char* str)</code> Αντιγράφει το //str// στο string.  
-|  <code cpp></code>  |   | +|  <code cpp>string& insert (size_t pos, const string& str); 
- <code cpp></code>  |   | +string& insert (size_t pos, const char* str);</code> Ενθέτει ένα αντίγραφο του //str// στη θέση //pos// του //string//.  
- <code cpp></code>  |   | +|  <code cpp>string& erase (size_t pos = 0, size_t len = npos); 
-|  <code cpp></code>   |+iterator erase (iterator p); 
 +iterator erase (iterator first, iterator last);</code> Διαγράφει μέρος του //string//, μειώνοντας το μήκος του.  
 +|  <code cpp>string& replace (size_t pos,  size_t len,  const string& str); 
 +string& replace (size_t pos,  size_t len,  const char* s); 
 +string& replace (size_t pos,  size_t len,  const string& str, 
 +                 size_t subpos, size_t sublen); 
 +</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";
  
-|  <code cpp></code>   | +  str+=str2; 
- <code cpp></code>  |   | +  str.append(str3); 
- <code cpp></code>  |   | +  str.append(str4); 
- <code cpp></code>  |   | +  cout << str; 
-|  <code cpp></code>  |   | +} 
-|  <code cpp></code>    | +</code>
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   |+
  
 +<code cpp replace.cpp>
 +#include <iostream>
 +#include <string>
 +using namespace std;
  
 +int main () {
 +  string base="this is a test string.";
 +  string str2="n example";
 +  string str3="sample phrase";
 +  string str4="useful.";
 +
 +  string str=base;                // "this is a test string."
 +  str.replace(9,5,str2);          // "this is an example string." (1)
 +  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)
 +  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 cpp>const char* c_str() const;</code>  | Επιστρέφει ένα //C-string// από ένα αντικείμενο της κλάσης //string// |
 +|  <code cpp>const char* data() const;</code>  | Ανάλογη με την //c_str()// με την διαφορά ότι η σειρά χαρακτήρων που προκύπτει δεν τερματίζεται με τον χαρακτήρα '\0' |
 +|  <code cpp>size_t copy (char* s, size_t len, size_t pos = 0) const;</code>  | Αντιγράφει ένα υπο-//string// του τρέχοντος //string// στον πίνακα που δείχνει ο δείκτης //s//. |
 +|  <code cpp>size_t find (const string& str, size_t pos = 0) const;
 +size_t find (const char* str, size_t pos = 0) const;
 +size_t find (const char* str, size_t pos, size_t n) const;
 +size_t find (char c, size_t pos = 0) const;
 +</code>  | Αναζητά στο //string// την πρώτη εμφάνιση του //string str// ή του χαρακτήρα //c// ξεκινώντας την αναζήτηση από την θέση pos.  |
 +|  <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)