std::string třídy v C++
C++ má ve své definici způsob, jak reprezentovat a posloupnost znaků jako objekt třídy . Tato třída se nazývá std:: string. Třída string ukládá znaky jako posloupnost bajtů s funkcí povolení přístup k jednobajtovému znaku .
Řetězec vs pole znaků
| Tětiva | Char Array |
|---|---|
| Řetězec je a třída, která definuje objekty které jsou reprezentovány jako proud postav. | Pole znaků je prostě an pole znaků který může být ukončen znakem null. |
| V případě řetězců je to paměť dynamicky přidělovány . Více paměti lze alokovat za běhu na vyžádání. Protože není předem přidělena žádná paměť, žádná paměť není plýtvána . | Velikost pole znaků musí být přiděleno staticky V případě potřeby nelze za běhu přidělit více paměti. Nepoužité přidělené paměť je také plýtvána |
| Protože řetězce jsou reprezentovány jako objekty, žádný rozpad pole dochází. | Tady je ohrožení rozpad pole v případě pole znaků. |
| Struny jsou pomalejší ve srovnání s implementací než pole znaků. | Implementace pole znaků je rychlejší než std:: řetězec. |
| Třída String definuje řadu funkcí které umožňují mnohonásobné operace na řetězcích. | Pole znaků nenabízejte mnoho vestavěné funkce manipulovat se strunami. |
Operace na řetězcích
1) Vstupní funkce
| Funkce | Definice |
|---|---|
| getline() | Tato funkce se používá k uložení proudu znaků zadaných uživatelem do paměti objektu. |
| zatlačit zpátky() | Tato funkce se používá k zadání znaku na konci řetězce. |
| pop_back() | Tato funkce, zavedená z C++11 (pro řetězce), se používá k odstranění posledního znaku z řetězce. |
Pří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ýstup
The initial string is : The string after push_back operation is : s The string after pop_back operation is :
Časová složitost: O(1)
Vesmírná složitost: O(n) kde n je velikost řetězce
2) Kapacitní funkce
| Funkce | Definice |
|---|---|
| kapacita() | Tato funkce vrací kapacitu přidělenou řetězci, která může být rovna nebo větší než velikost řetězce. Je přiděleno další místo, takže po přidání nových znaků do řetězce lze operace provádět efektivně. |
| změnit velikost () | Tato funkce mění velikost řetězce, velikost lze zvětšit nebo zmenšit. |
| délka() | Tato funkce zjistí délku řetězce. |
| shrink_to_fit() | Tato funkce snižuje kapacitu řetězce a rovná se minimální kapacitě řetězce. Tato operace je užitečná pro úsporu další paměti, pokud jsme si jisti, že není třeba provádět žádné další přidávání znaků. |
Příklad:
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;> }> |
Výstup
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á složitost: O(1)
Vesmírná složitost: O(n) kde n je velikost řetězce
3) Funkce iterátoru
| Funkce | Definice |
|---|---|
| začít() | Tato funkce vrací iterátor na začátek řetězce. |
| konec() | Tato funkce vrací iterátor na další konec řetězce. |
| rbegin() | Tato funkce vrací zpětný iterátor ukazující na konec řetězce. |
| poskytnout() | Tato funkce vrací zpětný iterátor ukazující na předchozí začátek řetězce. |
| cbegin() | Tato funkce vrací konstantní iterátor ukazující na začátek řetězce, nelze ji použít k úpravě obsahu, na který ukazuje. |
| trochu() | Tato funkce vrací konstantní iterátor ukazující na další konec řetězce, nelze ji použít k úpravě obsahu, na který ukazuje. |
| crbegin() | Tato funkce vrací konstantní reverzní iterátor ukazující na konec řetězce, nelze ji použít k úpravě obsahu, na který ukazuje. |
| krédo() | Tato funkce vrací konstantní reverzní iterátor ukazující na předchozí začátek řetězce, nelze ji použít k úpravě obsahu, na který ukazuje. |
Algoritmus:
- Deklarujte řetězec
- Zkuste iterovat řetězec pomocí všech typů iterátorů
- Zkuste modifikaci prvku řetězce.
- Zobrazit všechny iterace.
Pří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ýstup
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á složitost: O(1)
Vesmírná složitost: O(n) kde n je velikost řetězce
4) Manipulační funkce:
| Funkce | Definice |
|---|---|
| copy (pole znaků, len, pos) | Tato funkce zkopíruje podřetězec v cílovém poli znaků uvedeném v jejích argumentech. Ke spuštění kopírování jsou zapotřebí 3 argumenty, pole cílových znaků, délka, která se má zkopírovat, a počáteční pozice v řetězci. |
| swap() | Tato funkce vymění jeden řetězec za jiný |
Příklad:
CPP
// 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ýstup
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íš číst: Řetězcová třída C++ a její aplikace