std::to_string in C++ Last Updated : 24 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_string(val);Parametersval: It is the value which we have to convert into string. It can be of any numeric data type like integer, long long, double, float, long double.Return ValueA string containing the representation of val as a sequence of characters.Example of std::to_string() C++ // C++ Program to show how to use std::to_string // for converting any numerical value #include <bits/stdc++.h> using namespace std; int main() { int num = 42; double pi = 3.14159; float fnum = 1.234f; // Converts integer to string string str1 = to_string(num); // Converts double to string string str2 = to_string(pi); // Converts float to string string str3 = to_string(fnum); cout << "Numbers as String: " << endl; cout << str1 << endl; cout << str2 << endl; cout << str3 << endl; return 0; } OutputNumbers as String: 42 3.141590 1.234000 Comment More infoAdvertise with us Next Article std::to_string in C++ R Rohit Thapliyal Improve Article Tags : Misc C++ STL cpp-strings-library Practice Tags : CPPMiscSTL Similar Reads std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po 2 min read std::string::size() in C++ The std::string::size() function in C++ is a built-in method of the std::string class that is used to determine the number of characters in the string. All characters up to the null character are considered while calculating the size of the string.In this article, we will learn about string::size() 2 min read std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::stof in C++ Parses string interpreting its content as a floating-point number, which is returned as a value of type float. Syntax : float stof (const string& str, size_t* idx = 0); float stof (const wstring& str, size_t* idx = 0); Parameters : str : String object with the representation of a floating-po 2 min read std::string::clear in C++ The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing CPP // 1 min read Like