×

Search anything:

std:to_string and std::to_wstring in C++

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article, we have explored the functions std:to_string and std::to_wstring in C++ STL along with the difference between string and wstring (wide string).

Table of content

  • std::to_string
  • std::to_wstring
  • Difference between string and wstring

In short: std:to_string is used to convert a numeric datatype to string while std::to_wstring is used to convert a numeric datatype to wide string (wstring).

std::to_string

std::to_string in C++ is a Template used to convert any data type to
string.

It can be used on all data types such as:

  1. int
  2. float
  3. double
  4. long
  5. long double

The syntax used is as follows ::

string to_string(datatype variable name)

For example conversion of double can be done as follow:

CODE of conversion

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
  double a=92.863;
  char c[10]="hello";
  
   std::string str1 = std::to_string(a);
   // double a is converted into string 
         std::cout << str1<<endl ;
         cout<<(a);
         cout << typeid(a).name() << endl;
       
    return 0;
    
  
}

output

92.863000
92.863d

typeid

Now to check wether the data type is converted to string or not we can use
syntax of type id is as follows
typeif(variable name).name();

COMPLEXITY
It is a process which can be converted in O(1) time complexity.

Application
one of the most common appplication used is to find out the index of a particular element in the string after conversion.

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
 // Converting number to string
    std::string str = std::to_string(732);
 
    // Finding 7 in the number
    std::cout << "7 is at position " << str.find('7') + 1;
       
    return 0;
}

OUTPUT

7 is at position 1

As we can see +1 is added after str.find() because it indexing is starts from 0.

std::to_wstring

This function is used to convert the numerical value to the wide string i.e. it parses a numerical value of datatypes int ,float,char,double to a wide string. In short it converts all data types in w string datatype by typecasting the numeric values stored in it.

Syntax of conversion
Syntax :

wstring to_wstring (int variable name);

The only input it takes is variable name and as a result it converts the given data to to wstring.

code of conversion

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

int main ()
{
 

float x = 3.1415926;
int a = 5 , b = 9;
double y = 6.29;
 
// numerical values being typecasted into wstring

wstring perfect = to_wstring(a+b) +
                    L" is a number";
wstring num = to_wstring(y/x) +
                    L"is division of two numbers";
 
// Printing the typecasted wstring

wcout << perfect << L'\n';
wcout << num <<L'\n';
return 0;
}
14 is a number
2.002169 is division of two numbers

To print out come we use wcout instead of cout ,therefore we have to include #include<algorithm> for proper functioning.

Applications
It can be used for calculation to be used in report statement such as average value , we can directly use this function to convert numerical values rather than calculating it.

// These header files contains wcout and wstring
#include <iostream>
#include <string>	

using namespace std;

// Driver code
int main ()
{


int a = 60 , b = 45;


wstring rep = L"Number of section = " + to_wstring(a/b);
wstring sec_rep = to_wstring(b) +
			L" is the number of students in each section";


wcout << rep << L'\n';
wcout << sec_rep << L'\n';
return 0;
    
}

output

number of section is 1
45 is the number of student in the class.

Difference between string and wstring

  • std::string holds collection of char_t (char) to represent a string. The type char strictly holds only the standard ASCII characters (0-255).
  • std::wstring holds collection of wchar_t (wide chars)to represent a string. The type wchar_t holds the character in UTF (2/4 bytes) format. They are used to store unicode.

On parallel lines you can differentiate between std::cout & std::wcout, you can understand std::cout cannot work with std::wstring.

With this article at OpenGenus, you must have the complete idea of std:to_string and std::to_wstring in C++.

std:to_string and std::to_wstring in C++
Share this