cpp:strings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cpp:strings [2017/05/02 14:44] – [Aκολουθία χαρακτήρων που περιέχει το χαρακτήρα '\0' στο τέλος] gthanos | cpp:strings [2017/05/08 05:26] – [Διάτρεξη] gthanos | ||
---|---|---|---|
Line 17: | Line 17: | ||
{{ : | {{ : | ||
- | Στη C++ είναι διαθέσιμες οι συναρτήσεις διαχείρισης συμβολοσειρών που προέρχονται από τη C, όπως παρακάτω: | + | Στη C++ είναι διαθέσιμες οι συναρτήσεις διαχείρισης συμβολοσειρών που προέρχονται από τη C, όπως |
| <code cpp>char *strcpy(char *dest, const char *src);</ | | <code cpp>char *strcpy(char *dest, const char *src);</ | ||
Line 29: | Line 29: | ||
===== Η κλάση String ===== | ===== Η κλάση String ===== | ||
+ | |||
+ | Η // | ||
+ | |||
+ | Δείτε το παρακάτω παράδειγμα χρήσης της κλάσης //string//: | ||
+ | <code cpp string.cpp> | ||
+ | int main(int argc, char *argv[]) { | ||
+ | string str = "Hello World!"; | ||
+ | cout << str << endl; | ||
+ | str.append(" | ||
+ | cout << str << endl; | ||
+ | | ||
+ | str = "Hello Wordl!"; | ||
+ | str += " How are you?"; | ||
+ | cout << str << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Οι βασικές μέθοδοι της κλάσης string είναι οι εξής: | ||
+ | |||
+ | ===== Κατασκευαστές ===== | ||
+ | |||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | |||
+ | Παράδειγμα χρήσης κατασκευαστών | ||
+ | <code cpp strings_constructor.cpp> | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() { | ||
+ | string str = "Hello World!"; | ||
+ | string copy(str); | ||
+ | string substring(str, | ||
+ | const char *p = str.c_str(); | ||
+ | string fromCString(p); | ||
+ | string fromCSequence(p, | ||
+ | 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 cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp> | ||
+ | | <code cpp>void resize (size_t n); και | ||
+ | void resize (size_t n, char c);</ | ||
+ | | <code cpp>void reserve (size_t n = 0);</ | ||
+ | | <code cpp>void clear();</ | ||
+ | | <code cpp>bool empty() const;</ | ||
+ | |||
+ | ==== Πρόσβαση σε χαρακτήρες του αλφαριθμητικού ==== | ||
+ | |||
+ | | <code cpp> | ||
+ | const char& operator[] (size_t pos) const;</ | ||
+ | | <code cpp> | ||
+ | const char& at (size_t pos) const;</ | ||
+ | | <code cpp> | ||
+ | const char& back() const;</ | ||
+ | | <code cpp> | ||
+ | const char& front() const;</ | ||
+ | |||
+ | Στις όλες παραπάνω περιπτώσεις εάν το //string// είναι //const// επιστρατεύεται η //const// έκδοση της συνάρτησης. | ||
+ | |||
+ | ==== Διάτρεξη ==== | ||
+ | |||
+ | | <code cpp> | ||
+ | const_iterator begin() const;</ | ||
+ | | <code cpp> | ||
+ | const_iterator end() const;</ | ||
+ | | <code cpp> | ||
+ | const_reverse_iterator rbegin() const;</ | ||
+ | | <code cpp> | ||
+ | const_reverse_iterator rend() const;</ | ||
+ | |||
+ | === Παράδειγμα χρήσης iterator === | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main (){ | ||
+ | string str = "Hello World!"; | ||
+ | for( string:: | ||
+ | cout << *it; | ||
+ | cout << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === Παράδειγμα χρήσης reverse_iterator === | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main (){ | ||
+ | string str = "Hello World!"; | ||
+ | for (string:: | ||
+ | cout << *rit; | ||
+ | cout << endl; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Μεταβολή του αλφαριθμητικού ==== | ||
+ | |||
+ | | <code cpp> | ||
+ | string& operator+= (const char* s); | ||
+ | string& operator+= (char c);</ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | |||
+ | |||
+ | === Σύγκριση, | ||
+ | |||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
+ | | <code cpp></ | ||
cpp/strings.txt · Last modified: 2021/04/27 04:45 (external edit)