logo

std::string trieda v C++

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 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 viac pamäte nie je možné prideliť v čase spustenia, ak je to potrebné. Nepoužité pridelené plytvá sa aj pamäťou
Ako reťazce sú reprezentované ako objekty žiadny rozpad poľa sa 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

FunkciaDefiní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:

ako previesť celé číslo na reťazec v jave
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á zložitosť: O(1)

v ktorom roku bol vynájdený počítač

Priestorová zložitosť: O(n) kde n je veľkosť reťazca

2) Kapacitné funkcie

FunkciaDefiní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, ktorú možno zväčšiť alebo zmenšiť.
dĺžka ()Táto funkcia zistí dĺžku reťazca.
shrink_to_fit()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:

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á zložitosť: O(1)

Priestorová zložitosť: O(n) kde n je veľkosť reťazca

3) Funkcie iterátora

FunkciaDefiní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, ktorý nemožno použiť na úpravu obsahu, na ktorý ukazuje.
niekoľko ()Táto funkcia vracia konštantný iterátor ukazujúci na ďalší koniec reťazca, ktorý nemožno použiť na úpravu obsahu, na ktorý ukazuje.
crbegin()Táto funkcia vracia konštantný reverzný iterátor ukazujúci na koniec reťazca, ktorý nemožno 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, ktorý nemožno použiť na úpravu obsahu, na ktorý ukazuje.

Algoritmus:

  1. Deklarujte reťazec
  2. Skúste iterovať reťazec pomocou všetkých typov iterátorov
  3. Skúste modifikáciu prvku reťazca.
  4. 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:'<<str<<'n';  // 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<<'n';    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<<'n';  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á zložitosť: O(1)

ahoj svet java

Priestorová zložitosť: O(n) kde n je veľkosť reťazca

4) Manipulačné funkcie:

bash premenná
FunkciaDefinícia
copy('char array' len pos) Táto funkcia skopíruje podreťazec v poli cieľových znakov, ktorý je uvedený v jej argumentoch. Na skopírovanie sú potrebné 3 argumenty dĺžky cieľového poľa znakov a na spustenie kopírovania počiatočná pozícia v reťazci.
vymeniť ()Táto funkcia zamieňa jeden reťazec za iný

Prí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í si prečítať: C++ Stringová trieda a jej aplikácie