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
Last revisionBoth sides next revision
cpp:strings [2017/05/08 05:26] – [Διάτρεξη] gthanoscpp:strings [2021/04/27 05:45] gthanos
Line 27: Line 27:
 | <code cpp>char *strstr(const char *haystack, const char *needle);</code> | Επιστρέφει ένα δείκτη στην πρώτη εμφάνιση του ''needle'' στο ''heystack''. Eπιστρέφει NULL εάν δεν εμφανίζεται το ''needle'' στο ''heystack''. | | <code cpp>char *strstr(const char *haystack, const char *needle);</code> | Επιστρέφει ένα δείκτη στην πρώτη εμφάνιση του ''needle'' στο ''heystack''. Eπιστρέφει NULL εάν δεν εμφανίζεται το ''needle'' στο ''heystack''. |
 | <code cpp>char *strstr(const char *haystack, const char *needle);</code> | Επιστρέφει ένα δείκτη στην τελευταία εμφάνιση του ''needle'' στο ''heystack''. Επιστρέφει NULL εάν δεν εμφανίζεται το ''needle'' στο ''heystack''. | | <code cpp>char *strstr(const char *haystack, const char *needle);</code> | Επιστρέφει ένα δείκτη στην τελευταία εμφάνιση του ''needle'' στο ''heystack''. Επιστρέφει NULL εάν δεν εμφανίζεται το ''needle'' στο ''heystack''. |
- 
-===== Η κλάση String ===== 
- 
-Η //standard// βιβλιοθήκη της C++ διαθέτει την κλάση [[http://www.cplusplus.com/reference/string/string/|string]] η οποία έχει το πλεονέκτημα ότι δεν χρειάζεται να σας απασχολεί η δέσμευση της απαραίτητης μνήμης για την αποθήκευση των αλφαριθμητικών. 
- 
-Δείτε το παρακάτω παράδειγμα χρήσης της κλάσης //string//: 
-<code cpp string.cpp> 
-int main(int argc, char *argv[]) { 
-  string str = "Hello World!"; 
-  cout << str  << endl; 
-  str.append(" How are you?"); 
-  cout << str << endl; 
-   
-  str = "Hello Wordl!"; 
-  str += " How are you?"; 
-  cout << str << endl; 
-} 
-</code> 
- 
-Οι βασικές μέθοδοι της κλάσης string είναι οι εξής: 
- 
-===== Κατασκευαστές ===== 
- 
-| <code cpp>string();</code> | default constructor  
-| <code cpp>string (const string& str);</code>  | copy constructor  | 
-| <code cpp>string (const string& str, size_t pos, size_t len = npos);</code> | substring constructor | 
-| <code cpp>string (const char* s);</code>  | from c-string constructor  | 
-| <code cpp>string (const char* s, size_t n);</code>  | from c-string sequence |  
-| <code cpp>string (size_t n, char c);</code>  | fill with c | 
- 
-Παράδειγμα χρήσης κατασκευαστών 
-<code cpp strings_constructor.cpp> 
-#include <iostream> 
-using namespace std; 
- 
-int main() { 
-  string str = "Hello World!"; 
-  string copy(str);  
-  string substring(str, 0, 5); 
-  const char *p = str.c_str(); 
-  string fromCString(p); 
-  string fromCSequence(p, 8); 
-  string fillwithDollars(5, '$'); 
-   
-  cout << str << endl; 
-  cout << copy << endl; 
-  cout << substring << endl; 
-  cout << p << endl; 
-  cout << fromCString << endl; 
-  cout << fromCSequence << endl; 
-  cout << fillwithDollars << endl;   
-} 
-</code> 
- 
-===== Μέθοδοι ===== 
- 
-==== Χωρητικότητα και μέγεθος αλφαριθμητικού ==== 
- 
-| <code cpp>size_t size() const;</code>   | Επιστρέφει το μέγεθος του string  | 
-| <code cpp>size_t length() const;</code>   | Μέθοδος ανάλογη της μεθόδου size  | 
-| <code cpp>size_t capacity() const;</code>  | Χωρητικότητα του αλφαριθμητικού (σε bytes)  | 
-| <code cpp>void resize (size_t n); και  
- void resize (size_t n, char c);</code> | Επεκτείνεται το αλφαριθμητικό ώστε να έχει νέο μέγεθος ''n''. | 
-| <code cpp>void reserve (size_t n = 0);</code>  | Μεταβάλλει τη χωρητικότητα του αλφαριθμητικού. | 
-| <code cpp>void clear();</code> | Διαγράφει τα περιεχόμενα του αλφαριθμητικού  | 
-| <code cpp>bool empty() const;</code> | Επιστρέφει //true// εάν το αλφαριθμητικό είναι άδειο. | 
- 
-==== Πρόσβαση σε χαρακτήρες του αλφαριθμητικού ==== 
- 
-|  <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></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>  |   | 
-|  <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)