TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Convert int to string

C++ Convert int to string with C++ tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ int to string

There are three ways of converting an integer into a string:

C++ int to string
  • By using stringstream class
  • By using to_string() method
  • By using boost.lexical cast

Conversion of an integer into a string by using stringstream class.

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

The following are the operators used to insert or extract the data:

  • Operator >>: It extracts the data from the stream.
  • Operator <<: It inserts the data into the stream.

Let's understand the concept of operators through an example.

  • In the below statement, the << insertion operator inserts the 100 into the stream.
                          stream1 << 100;
  • In the below statement, the >> extraction operator extracts the data out of the stream and stores it in 'i' variable.
                          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++ int to string

In the above example, we created the k variable, and want to convert the value of k into a string value. We have used the stringstream class, which is used to convert the k integer value into a string value. We can also achieve in vice versa, i.e., conversion of string into an integer value is also possible through the use of stringstream class only.

Let's understand the concept of conversion of string into 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++ int to string

Conversion of an integer into a string by using to_string() method.

The to_string() method accepts a single integer and converts the integer value or other data type value into 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++ int to string

Conversion of an integer into a string by using a boost.lexical cast.

The boost.lexical cast provides a cast operator, i.e., boost.lexical_cast which converts the string value into an integer value or other data type value vice versa.

Let's understand the conversion of integer into string through an example.

#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
 int i=11;
 string str = boost::lexical_cast<string>(i);
cout<<"string value of integer i is :"<<str<<"\n";

}

Output

C++ int to string

In the above example, we have converted the value of 'i' variable into a string value by using lexical_cast() function.

Let's understand the conversion of string into integer through an example.

#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
int main()
{
string s="1234";
 int k = boost::lexical_cast<int>(s);
cout<<"Integer value of string s is : "<<k<<"\n";
}

Output

C++ int to string

In the above example, we have converted the string value into an integer value by using lexical_cast() function.

Next TopicC++ vs Python




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf