C++ Tutorial: Detailed Explanation of Converting to String

There are three methods to convert an integer to a string in C++:

C++ Tutorial: Detailed Explanation of Converting to String

  • Using the stringstream class

  • Using the to_string() method

  • Using boost.lexical_cast

👇Click to receive👇
👉C Language Knowledge Material Collection


Using the stringstream class to convert an integer to a string.

The stringstream class is defined in the header file. It is a stream class used to perform input and output operations on string-based streams.

The following are the operators used for inserting or extracting data:

  • Operator >>: Extracts data from the stream.

  • Operator <<: Inserts data into the stream.

Let’s understand these operators through an example.

  • In the following statement, the << insertion operator inserts 100 into the stream.

stream1 << 100;

  • In the following statement, the >> extraction operator extracts data from the stream and stores it in the variable ‘i’.

stream1 >> i;

Let’s understand through an example.

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

int main() {
   int k;
   cout << "Enter an integer value: ";
   cin >> k;
   stringstream ss;
   ss << k;
   string s;
   ss >> s;
   cout << "\n" << "An integer value is: " << k << "\n";
   cout << "String representation of an integer value is: " << s;
}

Output

C++ Tutorial: Detailed Explanation of Converting to String

In the above example, we created the variable k and wanted to convert the value of k to a string value. We used the stringstream class, which is used to convert the integer value k to a string value. We can also achieve the opposite conversion using the stringstream class, that is, converting a string to an integer value.

Let’s understand the concept of converting a string to a number through an example.

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

int main() {
   string number = "100";
   stringstream ss;
   ss << number;
   int i;
   ss >> i;
   cout << "The value of the string is: " << number << "\n";
   cout << "Integer value of the string is: " << i;
}

Output

C++ Tutorial: Detailed Explanation of Converting to String

Using the to_string() method to convert an integer to a string.

The to_string() method takes an integer and converts the integer value or other data type value to a string.

Let’s understand through an example:

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

int main() {
   int i = 11;
   float f = 12.3;
   string str = to_string(i);
   string str1 = to_string(f);
   cout << "String value of integer i is: " << str << "\n";
   cout << "String value of f is: " << str1;
}

Output

C++ Tutorial: Detailed Explanation of Converting to String

C++ Integer to String: Converting Integer to String Using to_string Function.

The to_string function takes an integer as a parameter and returns a string representing that integer.

Let’s understand through an example of converting an integer to a string:

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

int main(){
   int i = 11;
   string str = boost::lexical_cast<string>(i);
   cout << "The string value of integer i is: " << str << "\n";
}

Output:

C++ Tutorial: Detailed Explanation of Converting to String

To convert a string to an integer in C++: Using the boost::lexical_cast function to convert a string to an integer. The boost::lexical_cast function provides a conversion operator, which can convert a string value to an integer value or other data type values, and vice versa.

Let’s understand through an example of converting a string to an integer:

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

int main(){
   string s = "1234";
   int k = boost::lexical_cast<int>(s);
   cout << "The integer value of string s is: " << k << "\n";
}

Output:

C++ Tutorial: Detailed Explanation of Converting to String

In the above example, we used the lexical_cast() function to convert the string value to an integer value.

C++ Tutorial: Detailed Explanation of Converting to String

Popular Recommendations
  • CLion Tutorial – Creating Directories in CLion

  • C Language Algorithm – “Recovering Binary Search Tree” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of the Differences Between C++ and C#

Leave a Comment