Uno stringstream associa un oggetto stringa a un flusso permettendoti di leggere dalla stringa come se fosse un flusso (come cin). Per utilizzare stringstream, dobbiamo includere sstream file di intestazione. La classe stringstream è estremamente utile per analizzare l'input.
I metodi di base sono:
- clear()- Per cancellare il flusso. str()- Per ottenere e impostare un oggetto stringa il cui contenuto è presente nello stream. operatore <<- Aggiunge una stringa all'oggetto stringstream. operatore>>- Legge qualcosa dall'oggetto stringstream.
Esempi:
1. Contare il numero di parole in una stringa
Esempi:
Ingresso: Asipu Pawan Kumar
Produzione: 3Ingresso: Geek per geek Ide
Produzione: 4confrontare con la stringa
Di seguito è riportato il programma C++ per implementare l'approccio di cui sopra:
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>> parola)> >count++;> >return> count;> }> // Driver code> int> main()> {> >string s =>'geeks for geeks geeks '> >'contribution placements'>;> >cout <<>' Number of words are: '> << countWords(s);> >return> 0;> }> |
>
>Produzione
Number of words are: 6>
Complessità temporale: O(n*log(n)).
Spazio ausiliario: SU).
2. Stampa le frequenze delle singole parole in una stringa
Esempi:
Ingresso: Geeks For Geeks Quiz Geeks Quiz Pratica Pratica
Produzione: Per -> 1
Geek -> 3
Pratica -> 2
Quiz -> 2Ingresso: Stringa di frequenza della stringa di parole
Produzione: Frequenza -> 1
Stringa -> 2
Parola -> 1
Di seguito è riportato il programma C++ per implementare l'approccio di cui sopra:
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; // Utilizzato per spezzare le parole stringstream ss(st); // Per memorizzare singole parole string Word; while (ss>> Parola) FW[Parola]++; per (auto m: FW) cout<< m.first << '->'<< m.second << '
'; } // Driver code int main() { string s = 'Geeks For Geeks Ide'; printFrequency(s); return 0; }> |
>
>Produzione
For->1 Geeks-> 2 Ide-> 1>
Complessità temporale: O(n*log(n)).
convertire int in stringa
Spazio ausiliario: SU).
3. Converti intero in stringa
Poiché gli operatori di inserimento ed estrazione del flusso di stringhe funzionano con diversi tipi di dati. Ecco perché funziona bene con i numeri interi.
Inseriremo un numero intero nel flusso di stringhe e dopo averlo estratto in una stringa, quel valore intero diventerà una stringa.
Codice-
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; }> |
>
scarica youtube con vlc
>Produzione
1234>
Complessità temporale: O(n) ,n è la lunghezza dell'intero
Spazio ausiliario: O(n)
Rimozione di spazi da una stringa utilizzando Stringstream
Conversione di stringhe in numeri in C/C++