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:02] – [Μέθοδοι] gthanoscpp:strings [2017/05/08 05:51] – [Μεταβολή του αλφαριθμητικού] gthanos
Line 48: Line 48:
 Οι βασικές μέθοδοι της κλάσης string είναι οι εξής: Οι βασικές μέθοδοι της κλάσης string είναι οι εξής:
  
-==== Κατασκευαστές ====+===== Κατασκευαστές =====
  
 | <code cpp>string();</code> | default constructor  | <code cpp>string();</code> | default constructor 
Line 81: Line 81:
 </code> </code>
  
-==== Μέθοδοι ====+===== Μέθοδοι =====
  
-=== Χωρητικότητα και μέγεθος αλφαριθμητικού ===+==== Χωρητικότητα και μέγεθος αλφαριθμητικού ====
  
 | <code cpp>size_t size() const;</code>   | Επιστρέφει το μέγεθος του string  | | <code cpp>size_t size() const;</code>   | Επιστρέφει το μέγεθος του string  |
Line 94: Line 94:
 | <code cpp>bool empty() const;</code> | Επιστρέφει //true// εάν το αλφαριθμητικό είναι άδειο. | | <code cpp>bool empty() const;</code> | Επιστρέφει //true// εάν το αλφαριθμητικό είναι άδειο. |
  
-=== Πρόσβαση σε χαρακτήρες του αλφαριθμητικού === +==== Πρόσβαση σε χαρακτήρες του αλφαριθμητικού ====
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   | +
-|  <code cpp></code>  |   |+
  
-=== Μεταβολή του αλφαριθμητικού ===+|  <code cpp>char& operator[] (size_t pos); 
 +const char& operator[] (size_t pos) const;</code>  | Επιστρέφει μία αναφορά στη θέση //pos// του string. | 
 +|  <code cpp>char& at (size_t pos); 
 +const char& at (size_t pos) const;</code>  | Επιστρέφει μία αναφορά στη θέση //pos// του string. Πετάει //out_of_range exception// εάν δοθεί τιμή εκτός των ορίων του πίνακα.  | 
 +|  <code cpp>char& back(); 
 +const char& back() const;</code>  | Επιστρέφει μία αναφορά στον τελευταίο χαρακτήρα του //string//
 +|  <code cpp>char& front(); 
 +const char& front() const;</code>  | Επιστρέφει μία αναφορά στον πρώτο χαρακτήρα του //string//  |
  
-=== Σύγκριση, αναζήτηση, εξαγωγή υπο-αλφαριθμητικών ===+Στις όλες παραπάνω περιπτώσεις εάν το //string// είναι //const// επιστρατεύεται η //const// έκδοση της συνάρτησης.
  
 +==== Διάτρεξη ====
  
-=== Διάτρεξη ===+|  <code cpp>iterator begin(); 
 +const_iterator begin() const;</code>  | Επιστρέφει έναν //iterator// που δείχνει στον πρώτο χαρακτήρα του //string//
 +|  <code cpp>iterator end(); 
 +const_iterator end() const;</code>  | Επιστρέφει έναν //iterator// που δείχνει μετά τον τελευταίο χαρακτήρα του //string//
 +|  <code cpp>reverse_iterator rbegin(); 
 +const_reverse_iterator rbegin() const;</code>  | Επιστρέφει έναν //reverse iterator// που δείχνει στον τελευταίο χαρακτήρα του //string//
 +|  <code cpp>reverse_iterator rend(); 
 +const_reverse_iterator rend() const;</code>  | Επιστρέφει έναν //reverse iterator// που δείχνει πριν από τον πρώτο χαρακτήρα του //string//  |
  
 +=== Παράδειγμα χρήσης iterator ===
  
 +<code cpp>
 +#include <iostream>
 +#include <string>
 +using namespace std;
  
 +int main (){
 +  string str = "Hello World!";
 +  for( string::iterator it=str.begin(); it!=str.end(); ++it)
 +    cout << *it;
 +  cout << endl;
 +}
 +</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>
  
 +==== Μεταβολή του αλφαριθμητικού ====
  
 +|  <code cpp>string& operator+= (const string& str);
 +string& operator+= (const char* s);
 +string& operator+= (char c);</code>  | Επεκτείνει το αλφαριθμητικό προσθέτοντας τους επιπλέον χαρακτήρες του ορίσματος στο τέλος του. Επιστρέφει μία αναφορά στο τρέχον αντικείμενο. |
 +|  <code cpp>string& append (const string& str);
 +string& append (const char* str);</code>  | Επεκτείνει το αλφαριθμητικό ενθέτοντας ένα αντίγραφο του //str// στο τέλος του.  |
 +|  <code cpp>string& assign (const string& str);
 +string& assign (const char* str)</code>  | Αντιγράφει το //str// στο string.  |
 +|  <code cpp>string& insert (size_t pos, const string& str);
 +string& insert (size_t pos, const char* str);</code>  | Ενθέτει ένα αντίγραφο του //str// στη θέση //pos// του //string// |
 +|  <code cpp>string& erase (size_t pos = 0, size_t len = npos);
 +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></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></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
 +|  <code cpp></code>  |   |
  
  
cpp/strings.txt · Last modified: 2021/04/27 04:45 (external edit)