stringstream v C++ a jeho aplikáciách

Prúd reťazcov spája objekt typu reťazec s prúdom, ktorý vám umožňuje čítať z reťazca, ako keby to bol prúd (napríklad cin). Ak chcete použiť stringstream, musíme zahrnúť prúd hlavičkový súbor. Trieda stringstream je mimoriadne užitočná pri analýze vstupu.

Základné metódy sú:

    clear()- Vymazanie streamu. str()- Získať a nastaviť reťazcový objekt, ktorého obsah je prítomný v streame. operátor < <- Pridajte reťazec do objektu stringstream. operátor>>- Prečítajte si niečo z objektu stringstream.

Príklady:

1. Spočítajte počet slov v reťazci

Príklady:

Vstup: Asipu Pawan Kumar
Výkon: 3

Vstup: Geeks For Geeks Ide
Výkon: 4

Nižšie je uvedený program C++ na implementáciu vyššie uvedeného prístupu -

C++




// C++ program to count words in> // a string using stringstream.> #include> #include> #include> using> namespace> std;> int> countWords(string str)> {> > // Breaking input into word> > // using string stream> > > // Used for breaking words> > stringstream s(str);> > > // To store individual words> > string word;> > int> count = 0;> > while> (s>> slovo)> > count++;> > return> count;> }> // Driver code> int> main()> {> > string s => 'geeks for geeks geeks '> > 'contribution placements'> ;> > cout < <> ' Number of words are: '> < < countWords(s);> > return> 0;> }>

Výkon

 Number of words are: 6 

Časová zložitosť: O(n*log(n)).

Pomocný priestor: O(n).

2. Vytlačte frekvencie jednotlivých slov v reťazci

Príklady:

Vstup: Geeks For Geeks Kvíz Geeks Quiz Practice
Výkon: Pre -> 1
Geekovia -> 3
Cvičenie -> 2
Kvíz -> 2

Vstup: Slovný reťazec Frekvencia reťazca
Výkon: Frekvencia -> 1
Reťazec -> 2
Slovo -> 1

Nižšie je uvedený program C++ na implementáciu vyššie uvedeného prístupu -

C++




// C++ program to demonstrate use> // of stringstream to count> // frequencies of words.> #include> using> namespace> std;> void> printFrequency(string st)> {> > // Each word it mapped to> > // it's frequency> > mapint>FW; // Používa sa na delenie slov stringstream ss(st); // Na uloženie jednotlivých slov string Word; while (ss>> Word) FW[Word]++; pre (auto m : FW) cout < < m.first < < '->' < < m.second < < ' '; } // Driver code int main() { string s = 'Geeks For Geeks Ide'; printFrequency(s); return 0; }>

Výkon

For->1 Geeks-> 2 Ide-> 1 

Časová zložitosť: O(n*log(n)).

Pomocný priestor: O(n).

3. Preveďte celé číslo na reťazec

Pretože operátori vkladania a extrakcie reťazca reťazcov pracujú s rôznymi typmi údajov. Preto to funguje dobre s celými číslami.

Do reťazca reťazcov vložíme celé číslo a po jeho extrahovaní do reťazca sa táto celočíselná hodnota stane reťazcom.

kód-

C++




// C++ program to demonstrate the> // use of a stringstream to> // convert int to string> #include> #include> using> namespace> std;> // Driver code> int> main()> {> > int> val=123;> > // object from the class stringstream> > stringstream geek;> > // inserting integer val in geek stream> > geek < < val;> > // The object has the value 123> > // and stream it to the string x> > string x;> > geek>> x;> > // Now the string x holds the> > // value 123> > cout <'4' < return 0; }>

Výkon

1234 

Časová zložitosť: O(n) ,n je dĺžka celého čísla

Pomocný priestor: O(n)

Odstránenie medzier z reťazca pomocou Stringstream
Prevod reťazcov na čísla v C/C++