cpp:strings
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:strings [2017/05/08 07:06] – [Σύγκριση, αναζήτηση, εξαγωγή υπο-αλφαριθμητικών] gthanos | cpp:strings [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 28: | Line 28: | ||
| <code cpp>char *strstr(const char *haystack, const char *needle);</ | | <code cpp>char *strstr(const char *haystack, const char *needle);</ | ||
- | ===== Η κλάση 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> | ||
- | string& append (const char* str);</ | ||
- | | <code cpp> | ||
- | string& assign (const char* str)</ | ||
- | | <code cpp> | ||
- | string& insert (size_t pos, const char* str);</ | ||
- | | <code cpp> | ||
- | iterator erase (iterator p); | ||
- | iterator erase (iterator first, iterator last);</ | ||
- | | <code cpp> | ||
- | 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; | ||
- | |||
- | int main () { | ||
- | string str=" | ||
- | string str2=" "; | ||
- | char[] str3=" | ||
- | string str4 = " | ||
- | |||
- | str+=str2; | ||
- | str.append(str3); | ||
- | str.append(str4); | ||
- | cout << str; | ||
- | } | ||
- | </ | ||
- | |||
- | <code cpp replace.cpp> | ||
- | #include < | ||
- | #include < | ||
- | using namespace std; | ||
- | |||
- | int main () { | ||
- | string base=" | ||
- | string str2=" | ||
- | 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> | ||
- | | <code cpp> | ||
- | | <code cpp> | ||
- | 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 cpp> | ||
- | 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 cpp> | ||
- | | <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 cpp cstring.cpp> | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | using namespace std; | ||
- | |||
- | int main () { | ||
- | string str (" | ||
- | char * cstr = new char [str.length()+1]; | ||
- | strcpy (cstr, str.c_str()); | ||
- | |||
- | char * p = strtok (cstr," | ||
- | while (p!=0) { | ||
- | cout << p << ' | ||
- | p = strtok(NULL," | ||
- | } | ||
- | delete[] cstr; | ||
- | } | ||
- | </ |
cpp/strings.txt · Last modified: 2021/04/27 04:45 (external edit)