This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
cpp:strings [2017/05/08 05:13] gthanos |
cpp:strings [2017/05/08 06:00] gthanos [Μεταβολή του αλφαριθμητικού] |
||
|---|---|---|---|
| Line 106: | Line 106: | ||
| Στις όλες παραπάνω περιπτώσεις εάν το //string// είναι //const// επιστρατεύεται η //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></ | + | | <code cpp>string& operator+= (const string& str); |
| - | | <code cpp></ | + | string& operator+= (const char* s); |
| - | | <code cpp></ | + | string& operator+= (char c);</ |
| - | | <code cpp></code> | + | | <code cpp>string& append (const string& str); |
| - | | <code cpp></ | + | string& append (const char* str);</ |
| - | | <code cpp></code> | + | | <code cpp>string& assign (const string& str); |
| - | | <code cpp></ | + | string& assign (const char* str)</ |
| - | | <code cpp></code> | + | | <code cpp>string& insert (size_t pos, const string& str); |
| - | | <code cpp></ | + | string& insert (size_t pos, const char* str);</ |
| - | | <code cpp></ | + | | <code cpp>string& erase (size_t pos = 0, size_t len = npos); |
| - | | <code cpp></ | + | iterator erase (iterator p); |
| - | | <code cpp></ | + | iterator erase (iterator first, iterator last);</ |
| - | | <code cpp></ | + | | <code cpp>string& replace (size_t pos, size_t len, const string& str); |
| - | | <code cpp></code> | + | string& replace (size_t pos, size_t len, const char* s); |
| + | string& replace (size_t pos, size_t len, const string& str, | ||
| + | | ||
| + | </ | ||
| + | | <code cpp>void swap (string& | ||
| - | === Σύγκριση, | + | <code cpp append.cpp> |
| + | #include < | ||
| + | #include < | ||
| + | using namespace std; | ||
| - | | <code cpp></ | + | int main () { |
| - | | <code cpp></ | + | string str=" |
| - | | <code cpp></ | + | string str2=" "; |
| - | | <code cpp></ | + | |
| - | | <code cpp></ | + | string str4 = " |
| - | | <code cpp></ | + | |
| - | | | + | |
| - | | | + | str.append(str3); |
| - | | <code cpp></ | + | |
| + | | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | # | ||
| + | # | ||
| + | using namespace std; | ||
| - | === Διάτρεξη === | + | int main () { |
| + | string base="this is a test string."; | ||
| + | string str2="n example"; | ||
| + | string str3=" | ||
| + | string str4=" | ||
| + | |||
| + | string str=base; | ||
| + | str.replace(9, | ||
| + | str.replace(19, | ||
| + | str.replace(8, | ||
| + | cout << str << endl; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code cpp chicken.cpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | main (){ | ||
| + | string chicken (" | ||
| + | string egg (" | ||
| + | cout << " | ||
| + | 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 cpp></ | | <code cpp></ | ||
| Line 147: | Line 235: | ||
| | <code cpp></ | | <code cpp></ | ||
| | <code cpp></ | | <code cpp></ | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||