Reťazec reťazcov v C++

Reťazec reťazcov v C++

Reťazec je typ dátovej štruktúry používanej na ukladanie znakov. Reťazenie reťazcov v C++ je jednou z najdiskutovanejších tém súvisiacich s reťazcami. Existuje viacero metód na zreťazenie reťazcov pomocou užívateľom definovaných metód a niekoľko metód na zreťazenie reťazcov pomocou vopred definovaných metód. Pozrime sa na všetky tieto metódy.

ReťazecConcatenation-(1)-(1)

Metódy zreťazenia reťazca

Existuje 6 spôsobov zreťazenia Reťazec ako je uvedené nižšie:



  1. Použitie funkcie append().
  2. Použitie operátora „+“.
  3. Použitie funkcie strcat().
  4. Použitie C++ pre Loop.
  5. Použitie dedičnosti.
  6. Použitie funkcie Friend a funkcie strcat().

1. Použitie funkcie append().

Funkcia append() je členskou funkciou std::string trieda . Pomocou tejto funkcie môžeme zreťaziť dva objekty std::string (reťazce štýlu C++), ako je uvedené v príklade nižšie.

Syntax:

string& string::append (const string& str);  Here,   str:   String to be appended. 

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou funkcie append():

C++




// C++ Program for string> // concatenation using append> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > // Appending the string.> > init.append(add);> > cout < < init < < endl;> > return> 0;> }>

Výkon

this is init added now 

2. Pomocou operátora „+“.

Toto je najjednoduchší spôsob zreťazenia dvoch reťazcov. The + operátor pridáva struny a vráti zreťazený reťazec. Táto metóda funguje iba pre reťazce štýlu C++ (objekty std::string) a nefunguje s reťazcami štýlu C (pole znakov).

Syntax:

string new_string = init + add; 

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou operátora „+“:

C++




// C++ Program for string> // concatenation using '+' operator> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > // Appending the string.> > init = init + add;> > cout < < init < < endl;> > return> 0;> }>

Výkon

this is init added now 

3. Použitie funkcie strcat().

Funkcia C++ strcat() je vstavaná funkcia definovaná v hlavičkový súbor. Táto funkcia spája dva reťazce teplo a pridať a výsledok sa uloží do teplo reťazec. Táto funkcia funguje iba pre reťazce v štýle C (polia znakov) a nefunguje pre reťazce v štýle C++ (objekty std::string).

Syntax:

char * strcat(char * init, const char * add); 

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou funkcie strcat():

C++




// C++ Program for string> // concatenation using strcat> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > char> init[] => 'this is init'> ;> > char> add[] => ' added now'> ;> > // Concatenating the string.> > strcat> (init, add);> > cout < < init < < endl;> > return> 0;> }>

Výkon

this is init added now 

4. Použitie pre slučku

Použitie slučky je jednou z najzákladnejších metód zreťazenia reťazcov. Tu pridávame prvky jeden po druhom, pričom prechádzame celým reťazcom a potom ďalší reťazec. Konečným výsledkom bude zreťazený reťazec vytvorený z oboch reťazcov.

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou cyklu for:

C++




// C++ Program for string> // concatenation using for loop> #include> using> namespace> std;> // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > string output;> > // Adding element inside output> > // from init> > for> (> int> i = 0; init[i] !=> ' '> ; i++)> > {> > output += init[i];> > }> > // Adding element inside output> > // fromt add> > for> (> int> i = 0; add[i] !=> ' '> ; i++)> > {> > output += add[i];> > }> > cout < < output < < endl;> > return> 0;> }>

Výkon

this is init added now 

5. Použitie dedičnosti

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou dedičnosti:

C++




// C++ program for string concatenation> // using inheritance> #include> #include> using> namespace> std;> > // Base class> class> base> {> > protected> :> > virtual> string concatenate(string &str1,> > string &str2) = 0;> };> > // Derive class> class> derive:> protected> base {> > public> :> > string concatenate (string &str1,> > string &str2)> > {> > string temp;> > temp = str1 + str2;> > return> temp;> > }> };> > // Driver code> int> main()> {> > string init(> 'this is init'> );> > string add(> ' added now'> );> > > // Create string object> > derive obj;> > > // Print string> > cout < < obj.concatenate (init, add);> > > return> 0;> }>

Výkon

this is init added now 

6. Použitie funkcie Friend Function a funkcie strcat().

Nižšie je uvedený program C++ na zreťazenie reťazcov pomocou funkcie friend a funkcie strcat():

C++




// C++ program for string concatenation> // using friend function and strcat()> #include> #include> using> namespace> std;> // Base class> class> Base {> > public> :> > char> init[100] => 'this is init'> ;> > char> add[100] => ' added now'> ;> > > friend> void> myfun(Base b);> };> > void> myfun (Base b)> {> > // Pass parameter to concatenate> > strcat> (b.init, b.add);> > > cout < < b.init;> }> // Driver code> int> main()> {> > > // Create object of base class> > Base b;> > > // pass b object to myfun() to print> > // the concatenated string> > myfun(b);> > > return> 0;> }>

Výkon

this is init added now 


Najlepšie Články

Kategórie

Zaujímavé Články