×

C++ Convert Int to String

In fact, the conversion of numbers to strings or vice versa represents a significant paradigm change. We often need to convert a number to a string or a string to a number in general, or more especially in competitive programming. But we can't do so fluently because we have inadequate knowledge of some crucial skills. In this article, a few approaches to doing this work are discussed.

Number to String Conversion in C++:

The three main ways to change a number into a string are as follows:

  • By using string Stream 
  • By using to_string()
  • By using boost lexical cast

First approach: String Streams:

In this method, a stream object is declared that first inserts a number as a stream into an object before using "str()" to carry out the internal conversion of a number to a string.

C++ Program:

#include<iostream> //header files
#include <sstream> 
#include <string>


using namespace std;
int main()
{
	//initializing an integer
	int value = 2001;


	ostringstream strng1;


	strng1 << value;


	// changing number into string
	string s1 = strng1.str();


	//printing the string
	cout << " The freshly created number string is: ";
	cout << s1 << endl;


	return 0;
}

Output:

The freshly created number string is: 2001
Time Complexity:  O(n)
Space Complexity:  O(n)

Second approach: to_string():

The to_string() method takes a number, which can be of any data type, and returns it as the specified string.

C++ Program:

#include <iostream> //header files
#include <string>


using namespace std;




int main()
{
	//initializing an integer number
	int inval = 25;
	
	//initializing a floating number
	float flval = 15.03;


	//changing int to string
	string strng_i= to_string (inval);


	// changing float to string
	string strng_f = to_string (flval);


	// printing the converted strings
	cout << "In string, the integer is: ";
	cout << strng_i << endl;
	cout << "In string, the float is: ";
	cout << strng_f << endl;


	return 0;
}

Output:

In string, the integer is: 25
In string, the float is: 15.030000
Time Complexity: O(n)
Auxiliary Space: O(n)

Third approach:  Boost Lexical Cast:

Similar to how string conversion works, the "lexical cast()" method stays the same, but the argument list changes when using "boost lexical cast," becoming "lexical_cast(num_var)".

C++ Program:

#include <boost/lexical_cast.hpp> //header files
#include <iostream>
#include <string> 
using namespace std;




int main()
{
	//initializing a floating number
	float fl_val = 17.6;


	//initializing an integer number
	int in_val = 21;


	// changing float to string
	string strng_f = boost::lexical_cast<string>(fl_val);


	//changing int to string
	string strng_i = boost::lexical_cast<string>(in_val);


	// printing the converted strings
	cout << "In string, the float is: ";
	cout << strng_f << endl;
	cout << "In string, the integer is: ";
	cout << strng_i << endl;


	return 0;
}

Output:

In string, the float is: 17.6
In string, the integer is: 21
Time Complexity: O(n)
Auxiliary Space: O(n)

Related Topics

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

4 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++ Polymorphism

Polymorphism refers to the fact that something exists in several forms. Polymorphism, in simple terms, is the ability of a message to be presented in several formats. A real-life example...

4 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

Processing strings using std string stream in C++

A string class object called std: string stream is used to streamline into various variables, just as files can pour into strings. This class's things make use of a string...

3 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

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.

What are local class and global class in C++

In C++, a class is a fundamental block that achieves object-oriented programming. The class holds its data members and member functions. Objects help to access the data elements and functions....

4 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

Upcasting and Downcasting in C++

With the help of various examples in the C++ programming language, this section will cover Upcasting and Downcasting. Upcasting and downcasting, on the other hand, are two forms of object typecasting. Consider...

3 minutes read.

How the value is passed in C++

Introduction: The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter...

5 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

How to create a library in C++

Before going on to the creation of a library, let’s understand its meaning. What is a library? In simple words, a library is a collection of numerous functions, methods, classes, header files...

7 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.