×

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

std::distance() in C++

The primary function of std::distance is to facilitate the total number of elements if we have two iterators. It is defined inside the header files. It has both magnitude and...

2 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

For Loop Examples in C++

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 of for loop In C++, a...

6 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Approach in C++

Object oriented programming languages like Java or C++ use a bottom-up approach that identifies each object first.  In the bottom-up approach, we create a small problem first, and try to...

6 minutes read.

C++ | C Plus Plus Data type

Data type in every language is very important. Data type means the different kinds of data that are supported by a particular programming language. A computer language’s data types specify the...

15 minutes read.

Scope Resolution Operator vs this Pointer in C++

In this tutorial, we will compare the Scope Resolution operation to this Pointer in C++ language. Scope Resolution Operator The Scope Resolution Operator in C++ programming language is usually denoted by (::)....

3 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

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...

2 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

Reserved Keywords in C++

What are reserved keywords in C++? There are a few keywords that cannot be used as identifiers as those words are reserved for some other purposes, such keywords are called reserved...

15 minutes read.

C++ Bitwise XOR Operator

Exclusive OR is another name for the bitwise XOR operator. The ‘^’ is used to indicate it. It operates at the bit level of the operands, as the name implies....

4 minutes read.

Initialize Vector in C++

Initialize Vector in C++  The following comparison operators are defined for vector and those are given below. ==, <, <=, !=, >,>=  This allows you to access the element of a vector using...

3 minutes read.