C++ má vo svojej definícii spôsob, ako reprezentovať a postupnosť znakov ako objekt triedy . Táto trieda sa nazýva std:: string. Trieda string ukladá znaky ako postupnosť bajtov s funkciou povolenia prístup k jednobajtovému znaku .
String vs Character Array
| Reťazec | Char Array |
|---|---|
| Reťazec je a trieda, ktorá definuje objekty ktoré sú reprezentované ako prúd znakov. | Pole znakov je jednoducho an pole znakov ktorý môže byť ukončený znakom null. |
| V prípade reťazcov je to pamäť dynamicky prideľované . Na požiadanie je možné prideliť viac pamäte za behu. Keďže nie je vopred pridelená žiadna pamäť, žiadna pamäť nie je zbytočná . | Veľkosť poľa znakov musí byť pridelené staticky , v prípade potreby nie je možné v čase spustenia prideliť viac pamäte. Nepoužité pridelené plytvá sa aj pamäťou |
| Keďže reťazce sú reprezentované ako objekty, žiadny rozpad poľa vyskytuje. | Existuje a hrozba rozpad poľa v prípade poľa znakov. |
| Struny sú pomalšie v porovnaní s implementáciou ako pole znakov. | Implementácia pole znakov je rýchlejšie než std:: string. |
| Trieda reťazca definuje množstvo funkcií ktoré umožňujú mnohonásobné operácie na reťazcoch. | Polia znakov neponúkať veľa vstavané funkcie manipulovať so strunami. |
Operácie na reťazcoch
1) Vstupné funkcie
| Funkcia | Definícia |
|---|---|
| getline() | Táto funkcia sa používa na uloženie prúdu znakov zadaných používateľom do pamäte objektu. |
| push_back() | Táto funkcia sa používa na vloženie znaku na koniec reťazca. |
| pop_back() | Táto funkcia, zavedená z C++ 11 (pre reťazce), sa používa na odstránenie posledného znaku z reťazca. |
Príklad:
CPP
// C++ Program to demonstrate the working of> // getline(), push_back() and pop_back()> #include> #include // for string class> using> namespace> std;> > // Driver Code> int> main()> {> >// Declaring string> >string str;> > >// Taking string input using getline()> >getline(cin, str);> > >// Displaying string> >cout <<>'The initial string is : '>;> >cout << str << endl;> > >// Inserting a character> >str.push_back(>'s'>);> > >// Displaying string> >cout <<>'The string after push_back operation is : '>;> >cout << str << endl;> > >// Deleting a character> >str.pop_back();> > >// Displaying string> >cout <<>'The string after pop_back operation is : '>;> >cout << str << endl;> > >return> 0;> }> |
>
>Výkon
The initial string is : The string after push_back operation is : s The string after pop_back operation is :>
Časová zložitosť: O(1)
Priestorová zložitosť: O(n) kde n je veľkosť reťazca
2) Kapacitné funkcie
| Funkcia | Definícia |
|---|---|
| kapacita() | Táto funkcia vracia kapacitu pridelenú reťazcu, ktorá môže byť rovnaká alebo väčšia ako veľkosť reťazca. Je pridelený ďalší priestor, takže keď sa do reťazca pridajú nové znaky, operácie sa dajú vykonávať efektívne. |
| zmeniť veľkosť () | Táto funkcia mení veľkosť reťazca, veľkosť je možné zväčšiť alebo zmenšiť. |
| dĺžka () | Táto funkcia zistí dĺžku reťazca. |
| zmenšiť, aby sa zmestili() | Táto funkcia znižuje kapacitu reťazca a rovná sa minimálnej kapacite reťazca. Táto operácia je užitočná na šetrenie ďalšej pamäte, ak sme si istí, že už nie je potrebné pridávať ďalšie znaky. |
Príklad:
celé číslo na reťazec java
CPP
// C++ Program to demonstrate the working of> // capacity(), resize() and shrink_to_fit()> #include> #include // for string class> using> namespace> std;> > // Driver Code> int> main()> {> >// Initializing string> >string str =>'geeksforgeeks is for geeks'>;> > >// Displaying string> >cout <<>'The initial string is : '>;> >cout << str << endl;> > >// Resizing string using resize()> >str.resize(13);> > >// Displaying string> >cout <<>'The string after resize operation is : '>;> >cout << str << endl;> > >// Displaying capacity of string> >cout <<>'The capacity of string is : '>;> >cout << str.capacity() << endl;> > >// Displaying length of the string> >cout <<>'The length of the string is :'> << str.length()> ><< endl;> > >// Decreasing the capacity of string> >// using shrink_to_fit()> >str.shrink_to_fit();> > >// Displaying string> >cout <<>'The new capacity after shrinking is : '>;> >cout << str.capacity() << endl;> > >return> 0;> }> |
nastavený js
>
>Výkon
The initial string is : geeksforgeeks is for geeks The string after resize operation is : geeksforgeeks The capacity of string is : 26 The length of the string is :13 The new capacity after shrinking is : 13>
Časová zložitosť: O(1)
Priestorová zložitosť: O(n) kde n je veľkosť reťazca
3) Funkcie iterátora
| Funkcia | Definícia |
|---|---|
| začať() | Táto funkcia vracia iterátor na začiatok reťazca. |
| koniec() | Táto funkcia vráti iterátor na ďalší koniec reťazca. |
| rbegin() | Táto funkcia vracia spätný iterátor ukazujúci na koniec reťazca. |
| render() | Táto funkcia vracia spätný iterátor ukazujúci na predchádzajúci začiatok reťazca. |
| cbegin() | Táto funkcia vracia konštantný iterátor ukazujúci na začiatok reťazca, nemožno ju použiť na úpravu obsahu, na ktorý ukazuje. |
| zopár() | Táto funkcia vracia konštantný iterátor ukazujúci na ďalší koniec reťazca, nemožno ju použiť na úpravu obsahu, na ktorý ukazuje. |
| crbegin() | Táto funkcia vracia konštantný reverzný iterátor ukazujúci na koniec reťazca, nemožno ju použiť na úpravu obsahu, na ktorý ukazuje. |
| Crend() | Táto funkcia vracia konštantný reverzný iterátor ukazujúci na predchádzajúci začiatok reťazca, nemožno ju použiť na úpravu obsahu, na ktorý ukazuje. |
Algoritmus:
- Deklarujte reťazec
- Skúste iterovať reťazec pomocou všetkých typov iterátorov
- Skúste modifikáciu prvku reťazca.
- Zobrazte všetky iterácie.
Príklad:
CPP
// C++ Program to demonstrate the working of> // begin(), end(), rbegin(), rend(), cbegin(), cend(), crbegin(), crend()> #include> #include // for string class> using> namespace> std;> > // Driver Code> int> main()> {> >// Initializing string`> >string str =>'geeksforgeeks'>;> > >// Declaring iterator> >std::string::iterator it;> > >// Declaring reverse iterator> >std::string::reverse_iterator it1;> >cout<<>'Str:'><'
'; // Displaying string cout << 'The string using forward iterators is : '; for (it = str.begin(); it != str.end(); it++){ if(it == str.begin()) *it='G'; cout << *it; } cout << endl; str = 'geeksforgeeks'; // Displaying reverse string cout << 'The reverse string using reverse iterators is ' ': '; for (it1 = str.rbegin(); it1 != str.rend(); it1++){ if(it1 == str.rbegin()) *it1='S'; cout << *it1; } cout << endl; str = 'geeksforgeeks'; //Displaying String cout<<'The string using constant forward iterator is :'; for(auto it2 = str.cbegin(); it2!=str.cend(); it2++){ //if(it2 == str.cbegin()) *it2='G'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content, but we can inc/dec-rement the iterator cout<<*it2; } cout<<'
'; str = 'geeksforgeeks'; //Displaying String in reverse cout<<'The reverse string using constant reverse iterator is :'; for(auto it3 = str.crbegin(); it3!=str.crend(); it3++){ //if(it2 == str.cbegin()) *it2='S'; //here modification is NOT Possible //error: assignment of read-only location //As it is a pointer to the const content, but we can inc/dec-rement the iterator cout<<*it3; } cout<<'
'; return 0; } //Code modified by Balakrishnan R (rbkraj000)> |
>
>Výkon
Str:geeksforgeeks The string using forward iterators is : Geeksforgeeks The reverse string using reverse iterators is : Skeegrofskeeg The string using constant forward iterator is :geeksforgeeks The reverse string using constant reverse iterator is :skeegrofskeeg>
Časová zložitosť: O(1)
Priestorová zložitosť: O(n) kde n je veľkosť reťazca
4) Manipulačné funkcie:
| Funkcia | Definícia |
|---|---|
| kopírovať (pole znakov, len, poz.) | Táto funkcia skopíruje podreťazec v poli cieľových znakov, ktorý je uvedený v jej argumentoch. Na spustenie kopírovania sú potrebné 3 argumenty, pole cieľových znakov, dĺžka kopírovania a počiatočná poloha v reťazci. |
| vymeniť () | Táto funkcia zamieňa jeden reťazec za iný |
Príklad:
CPP
prepínacia metóda java
// C++ Program to demonstrate the working of> // copy() and swap()> #include> #include // for string class> using> namespace> std;> > // Driver Code> int> main()> {> >// Initializing 1st string> >string str1 =>'geeksforgeeks is for geeks'>;> > >// Declaring 2nd string> >string str2 =>'geeksforgeeks rocks'>;> > >// Declaring character array> >char> ch[80];> > >// using copy() to copy elements into char array> >// copies 'geeksforgeeks'> >str1.copy(ch, 13, 0);> > >// Displaying char array> >cout <<>'The new copied character array is : '>;> >cout << ch << endl;> > >// Displaying strings before swapping> >cout <<>'The 1st string before swapping is : '>;> >cout << str1 << endl;> >cout <<>'The 2nd string before swapping is : '>;> >cout << str2 << endl;> > >// using swap() to swap string content> >str1.swap(str2);> > >// Displaying strings after swapping> >cout <<>'The 1st string after swapping is : '>;> >cout << str1 << endl;> >cout <<>'The 2nd string after swapping is : '>;> >cout << str2 << endl;> > >return> 0;> }> |
>
>Výkon
The new copied character array is : geeksforgeeks The 1st string before swapping is : geeksforgeeks is for geeks The 2nd string before swapping is : geeksforgeeks rocks The 1st string after swapping is : geeksforgeeks rocks The 2nd string after swapping is : geeksforgeeks is for geeks>
Musíte prečítať: C++ Stringová trieda a jej aplikácie