×

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

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

C++ | C Plus Plus While loop

In this article, we will discuss the C++ while loop with its syntax, use, key features, key points, pseudo code, and examples. What is the While Loop? The “while loop” is a...

4 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++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

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.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

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

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

swap() function in C++

Swap() function: swap() function in C++: swap() function is a pre-define function in c++ present in STL( Standard template library ). It is used to swap two numbers. It takes two mandatory...

6 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

4 Pillars of OOPs

OOPs OOPS means Object Oriented Programming System Structure. Object oriented Programming is defined as an approach that provides a way of modularizing programs by creating partitioned memory area for both data...

11 minutes read.

C++ Program to find largest subarray with 0 sum

Write a program to find the largest subarray that has a sum zero. The array contains positive and negative numbers. Print the length of the max subarray whose sum turns...

4 minutes read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.