How to Process strings using std::istringstream?


Introduction

In this tutorial, we will learn about the istringstream and how to use it for processing strings. istringstream is an object of string class defined in the <sstream> header file. It is used for reading from the stream strings. A stream is a flow of data (collection of characters) between input-output devices and programs.

The <sstream> header file defines 3 string class objects, which are as follows −

  • istringstream

  • ostringstream

  • Stringstream

All are used for separate operations, like istringstream is responsible for stream input, ostringstream is for stream output, and stringstream handles both input and output of the stream.

istringstreams class objects use buffers containing character sequence for processing the stream. To use the properties of the istringstream use this header file in your code.

#include <sstream>

Reading string streams integers up to white spaces

For reading the integers from the stream of string.

  • Create an integer string.

  • Create an istringstream object and pass the integer string variable as an argument.

  • Use extraction operator (>>) to retrieve the values of the string variable.

The >> operator performs a similar function of cin and to separate string into tokens it uses white spaces. It breaks the string into pieces on finding the white space.

Example

Here is the C++ implementation of istringstream. The istringstream objects process the number string variable till it encounters white space and after white space it stops.

#include <iostream>     
#include <sstream>      

int main (){

   std::string stringVar = "1 2 3 4 5";
   std::istringstream issVar (stringVar);
   int number;
   issVar >> number;
   std:: cout << "the first number is " << number;
  
   return 0; 
}

Output

the first number is 1

Read the whole integer stream

istringstream can be used to process the whole stream of strings by using loops. A loop can be for, depending on the code needed.

  • Create an integer string variable.

  • Create an istringstream object.

  • Pass the integer string variable as an argument to the istringstream object.

  • Create a new variable, number.

  • Use a while loop to process the string till the istringstream object is empty.

  • Use extraction operator (<<) for processing the number variable for istringstream object.

  • Print the result.

Example 1

Lets implement the above lines −

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
 
// Main code
int main(){
   // Input number string
   string strVar = "1 2 3 4";
 
   // creating object of istringstream
   istringstream issVar(strVar);
 
   // Variable for string the values of strVar
   int number;
 
   // Conditional statement for checking all integers of the string 
   while (issVar >> number){
 
      cout << "That stream has number: "
      << number << "\n";
   }
 
   cout << "That stream has reached the end successfully"
   << "\n";
   return 0;
}

Output

That stream has number: 1
That stream has number: 2
That stream has number: 3
That stream has number: 4
That stream has reached the end successfully

Example 2

The above code can change using if-else conditions for checking the successful processing of the stream.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

// Main code
int main(){
   // Number string
   string strVar = "1 2 3 4";
 
   // creating Object for istringstream
   istringstream issVar(strVar);
 
   // Variable for string the values
   int number;
 
   // Conditional statement to check the issVar
   while (issVar){
 
      //streaming the string using >> operator till white space
      issVar >> number;
 
      // processing till the end of the issVar
      if (issVar){
         cout << "The stream has number: "
         << number << "\n";
      } else {
         cout << "That stream has ended"
         << "\n";
      }
   }
 
   return 0;
}

Output

The stream has number: 1
The stream has number: 2
The stream has number: 3
The stream has number: 4
That stream has ended

Streaming an integer from a floating number using istringstream

In the above examples, we stream integers from a string. Here we stream floating and integer values from a string.

  • Create a string variable and initialize it with a floating number.

  • Create an object for the istringstream class and pass the string variable.

  • Declare an int-type variable for an integer.

  • Declare a float type variable for a floating data type.

  • Use extraction operator(<<) along with int and float variables for streaming the string.

Example

C++ implementation of above lines −

#include <iostream>
#include <sstream>
using std:: istringstream;
using namespace std;

int main(){
   //initializing string 
   string strVar = "45.65";

   // istringstream object containing string variable 
   std::istringstream issVar(strVar);

   //variable for the integer data type
   int number;
   
   //variable for the float data type
   double floatVar;

   issVar >> number;

   //printing the output while separating integer and float
   cout<< "Integer number is: " << number <<endl;
   issVar >> floatVar;
   cout << "Floating number is: " << floatVar;
   
   return 0;
}

Output

Integer number is: 45
Floating number is: 0.65

Streaming mixed data types using std:: istringstream

Till now, we streamed similar data types and white spaces. With istringstream you can stream different types of data at the same time.

In this example, we use a string containing different data types, such as string and number.

  • Create a string variable and initialize it.

  • Create a istingstream object and pass string variables to it.

  • Create a variable for each data type of the string.

  • Use << operator for streaming the string using istringstream object and variables.

  • Print the output with the variables declared in step c.

Example

Here is the C++ implementation.

#include <iostream>
#include <sstream> 

using namespace std;

int main() {
   //string variable 
   string strVar = "Tutorials Point 34";
     
   // variables to store different values of the string
   string first;
   string second;
   string third;
	
   //istringstream object
   std:: istringstream issVar;
   issVar.str(strVar);

   //streaming string
   issVar >> first;
   cout << "First word is : " << first << endl;

   issVar >> second;
   cout << "Second word is: " << second << endl;
 	
   issVar >> third;
   cout << "Third word is: " << third << endl;
 	
   return 0;
}

Output

First word is : Tutorials
Second word is: Point
Third word is: 34

Advantages of istringstream

  • It is used to stream a string.

  • It can stream multiple data types by creating istringstream objects.

  • It can be used to determine whether streaming is successful or not.

Conclusion

We have reached the end of this tutorial. In this tutorial, we processed strings through istringstream. Istringstream streams string using the extraction operator (>>) and its object. It can be used with different data types. This tutorial explores the meaning of istringstream and its uses in different scenarios.

Updated on: 03-Oct-2023

637 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements