How to Convert int to string C++

In this article, we will see Conversion from int to string C++ datatype using three different methods.

Introduction

In C++ programming, there are situations when it is required to convert one data type to another; one such conversion is from an int to a string.

Following are three ways to convert int to string datatype:

  1. stringstream class
  2. to_string() method
  3. boost::lexical_cast

int to string C++ Using stringstream class

Stringstream is a class that allows you to conduct input/output operations on string-based streams. The <and> operators are used to extract (<<) data from the stream and insert (>>) data into it.

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

int main() {
  int num = 05; // a variable of int data type
  
  string str; // a variable of str data type

  // using the stringstream class to insert an int and
  // extract a string
  stringstream a;  
  a << num;  
  a >> str;  

  cout << "The integer value is " << num << endl;  
  cout << "The string representation of the integer is " << str << endl;  
}

Output:

The integer value is 5
The string representation of the integer is 5

int to string Using to_string method

The to_string() method accepts any basic data type value and turns it into a string.

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

int main() {
  int num = 05; // a variable of int data type

  string str; // a variable of str data type

  // using to_string to convert an int into a string
  str = to_string(num);

  cout << "The integer value is " << num << endl;  
  cout << "The string representation of the integer is " << str << endl;  
}

Output:

The integer value is 5
The string representation of the integer is 5

int to string Using boost::lexical_cast

boost::lexical_cast gives a cast operator for converting an int to a string.

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

int main() {
  // a variable of int data type
  int num = 05;
  // a variable of str data type
  string str; 

  // using boost::lexical_cast<string> to convert an int into a string
  str = boost::lexical_cast<string>(num);

  cout << "The integer value is " << num << endl;  
  cout << "The string representation of the integer is " << str << endl;  
}

Output:

The integer value is 5
The string representation of the integer is 5

FAQs

What does stoi() do in C++?

The stoi() function in C++ converts a string to an integer value. The function is shorthand for “string to integer,” and it is use by C++ programmers to parse integers from strings.

How do I convert a char to a string in C++?

  1. string::string(size_type count, charT ch) Constructor to Convert a Char to a String
  2. Using push_back() Method to Convert a Char to a String
  3. the append() Method to Convert a Char to a String in C++
  4. Using the insert() Method to Convert a Char to a String in C++

stoi() vs atoi()

  • atoi() is a legacy C-style function. stoi() is added in C++ 11.
  • The atoi() works only for C-style strings (character array and string literal), stoi() works for both C++ strings, and C style strings
  • atoi() takes only one parameter and return an integer value.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. C++ String [With Examples]

Leave a Comment