Stringstream povezuje objekt niza s tokom, kar vam omogoča branje iz niza, kot da bi bil tok (kot cin). Če želimo uporabiti stringstream, moramo vključiti sstream naslovna datoteka. Razred stringstream je izjemno uporaben pri razčlenjevanju vnosa.
Osnovne metode so:
- clear()- Za čiščenje toka. str()- Za pridobitev in nastavitev objekta niza, katerega vsebina je prisotna v toku. operator <<- Dodajte niz objektu toka nizov. operator>>- Preberite nekaj iz objekta stringstream.
Primeri:
1. Preštejte število besed v nizu
Primeri:
Vnos: Asipu Pawan Kumar
Izhod: 3Vnos: Geeki za Geeke Ide
Izhod: 4
Spodaj je program C++ za implementacijo zgornjega pristopa-
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>> beseda)> >count++;> >return> count;> }> // Driver code> int> main()> {> >string s =>'geeks for geeks geeks '> >'contribution placements'>;> >cout <<>' Number of words are: '> << countWords(s);> >return> 0;> }> |
>
>Izhod
Number of words are: 6>
Časovna zahtevnost: O(n*log(n)).
Pomožni prostor: O(n).
2. Izpišite frekvence posameznih besed v nizu
Primeri:
Vnos: Geeks za Geeks kviz Geeks kviz Practice Practice
Izhod: Za -> 1
Geeki -> 3
Vaja -> 2
Kviz -> 2Vnos: Besedni niz Frekvenčni niz
Izhod: Pogostost -> 1
Niz -> 2
Beseda -> 1
Spodaj je program C++ za implementacijo zgornjega pristopa-
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; // Uporablja se za razbijanje besed stringstream ss(st); // Za shranjevanje posameznih besed string Word; medtem ko (ss>> Beseda) FW[Beseda]++; za (avto m : FW) izv<< m.first << '->'<< m.second << '
'; } // Driver code int main() { string s = 'Geeks For Geeks Ide'; printFrequency(s); return 0; }> |
>
>Izhod
For->1 Geeks-> 2 Ide-> 1>>>Časovna zahtevnost: O(n*log(n)).
Pomožni prostor: O(n).
3. Pretvori celo število v niz
Ker operaterji vstavljanja in ekstrakcije toka nizov delujejo z različnimi tipi podatkov. Zato dobro deluje s celimi števili.
V tok niza bomo vstavili celo število in po ekstrakciji tega v niz bo ta cela vrednost postala niz.
Koda-
C++
algoritem za razvrščanje vstavljanja
// 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; }> |
>Izhod
1234>
Časovna zahtevnost: O(n) ,n je dolžina celega števila
Pomožni prostor: O(n)
Odstranjevanje presledkov iz niza z uporabo Stringstream
Pretvarjanje nizov v številke v C/C++