×

Stringstream in C++ and its applications

In this tutorial, we will explore what the stringstream in C++ is. We will also learn its application.

What is stringstream?

With the aid of a stringstream, user can read from a string object as though it were a stream (like cin). sstream header file inclusion is required in order to use stringstream. Input parsing benefits greatly from the use of the stringstream class.

Common techniques are:

  1. clear() - It is used to empty the stream.
  2. str() – It can be used to retrieve and modify string objects with content that is present in the stream.
  3. operator << - To the stringstream object, add a string.
  4. operator >> - Read from the stringstream object using the operator >>.

Let's look at two string stream examples in below.

Example 1: We will separate the words into individual strings in this program.

C++ Code:

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


int main () 
{
   string str (" Hey Mr. don't move ");
   
   // declaring a string that will be used to preserve word each time.
   string abc; 
   
   // Using a string stream to separate input into words
   stringstream str_strm ( str ); 
   
   // Considering a vector that can contain our words.
   vector < string > splt; 
   
   // Using while loop
   while ( str_strm >> abc ) 
   {
      
      // pushing in abc
      splt.push_back ( abc );
   }
   
   // using for loop
   for ( int index = 0; index < splt.size(); index ++ )
      cout << splt [ index ] << endl;
}

Output:

Hey
Mr.
don't
move

Example 2: The total number of words in a given string will be determined by this program.

C++ Program:

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


int cntw ( string str )
{
	//Using a string stream to separate input into words
	stringstream a ( str );


	// declaring a string that will be used to preserve words individually
	string www;


	// declaring a variable to keep counts of words
	int ct = 0;
	
	// using while loop
	while ( a >> www )
		ct ++;
	return ct;
}


// main function 
int main ()
{
	string a = " My dream is to be a successful Technical Content Developer";
	cout << " The total number of words are: " << cntw ( a );
	return 0;
}

Output:

The total number of words are: 10

From the above two programs, we learn how to utilize stringstream in a C++ Code.


Related Topics

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

Template Specialization in C++

Template is a feature of C++. With the help of a template, we can write the code only once and use that code multiple times. For example, there is a...

4 minutes read.

std::min in C++

std::min in C++ std::min is specified in the program code, used to calculate the lowest amount that has been transferred. When there's more of someone who returns first of them. It's used...

2 minutes read.

C++ User Defined Exceptions

Overriding and inheriting exception class capabilities may be used to define the new exception. Exception handling can also be used with classes. We may also make an exception for user-defined...

4 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

Armstrong number using for loop in C++

What is For Loop? A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax that can be...

4 minutes read.

C++ For loop

C++ loop Statement C++ Loop statement allows us to repeat the execution of a statement or group of statements multiple times. The statement(s) repeat execution within loop until the condition of loop...

2 minutes read.

Naming Convention in C++

The first and most fundamental step a programmer takes to produce clean code is to name a file or a variable. This naming must be acceptable so that it serves...

5 minutes read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ cin and cout

In this article, we will discuss the C++ cin and cout with their library and examples. C++ Standard Input/Output: User-program communication is made possible by C++’s usage of input and output (I/O)...

5 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

Lambda Expression in C++

The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax...

3 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.