The <charconv> header contains several methods for converting character sequences to numerical information and vice versa. It is regarded as more effective than the <cstdlib> header file functions for the identical purpose. The functions given by the <charconv> header file are often quicker than the functions that are provided by the <cstdlib> header file.
It was introduced in C++17, and the major goal of developing this header was to enhance the complexity and speed of the code.
Functions in the <charconv> Header:
The charconv library has two functions:
1. to_chars
The to_chars method converts a number to its matching character representation and puts the output in a buffer.
Syntax:
It has the following syntax:
to_chars_result to_chars( char* initial, char* final, T val );
Parameters:
initial and final: Pointers to the start and end of the buffers where the output is going to be written.
val: The numeric value to be transformed.
Return Value:
It returns an object of type tocharsresult with two members:
- ptr: a reference to the first character following the end reference of the characters typed.
- ec: It is an error number that determines whether or not the process of conversion is completed. If the conversion succeeds, the code that indicates the problem will be set to errc; otherwise, it will be set to a different error code if an error occurs.
Example:
This code shows how to use the to_chars method from the <charconv> header to convert a value of integers to a character sequence.
Filename: Tochars. cpp
// Program to implement the use of to_chars() method
#include <array>
#include <charconv>
#include <iostream>
using namespace std;
int main()
{
// the buffer value for storing
array<char, 10> buffer;
// the int variable to convert
int value = 44;
// integer to character sequence conversion
auto res = to_chars(
buffer.data(), buffer.data() + buffer.size(), value);
// condition
if (res.ec == errc()) {
// If successful, print the converted value
cout << "The Converted value: "
<< string_view(buffer.data(),
res.ptr - buffer.data())
<< endl;
}
return 0;
}
Output:
The Converted value: 44
2. the function from_chars
The from_chars method is used for converting a number's character representation to its numeric form.
Parameters:
- initial and final: Indicates the start and end of the character to be transformed.
- value: The number that is to be converted's reference.
Return on Value
It returns the fromcharsresult object, which has two members:
- ptr: A pointer to the initial character following the last character entered it.
- ec: It is an error code that indicates whether or not the conversion was successful. If the process of conversion is successful, the error code is going to be set to errc; otherwise, if an error occurs, it will be set to an error code.
Example:
Filename: Fromchars.cpp
// the program to implement from_chars() function in c++
#include <charconv>
#include <iostream>
using namespace std;
int main()
{
// declaration of the string variable
string st = "48";
// variable for storing the converted value
int value;
// function calling from_chars()
auto res = from_chars(st.data(),
st.data() + st.size(), value);
// condition to check the conversion
if (res.ec == errc()) {
// If it is successful, then display the va;ue
cout << value << endl;
}
return 0;
}
Output:
Example 2:
Filename: Header.cpp
// the program to implement from_chars() function in c++
#include <charconv>
#include <iostream>
using namespace std;
int main()
{
// user input string value
string st = "126.78";
// variable for storing the converted value
double val = 0.0;
// Converting the string to a double value with std::from_chars The converted value is kept in 'val'
// 'ptr' is a reference to the character that appears in the string
// that comes after the converted value 'ec' is an error code that shows whether or not the conversion process was successful.
auto [ptr, ec] = from_chars(
st.data(), st.data() + st.size(), val);
// If no error occurs
if (ec == errc{}) {
// display of the converted value
cout << "The Converted value is: " << val << endl;
}
// If an error occurs
else {
//display of the error code
cout << "The Conversion is failed with an error code.?
<< static_cast<int>(ec) << endl;
}
return 0;
}
Output:
The Converted value is: 126.78
Benefits of Using <charconv> function
The following are some of the benefits of utilizing the <charconv> library functions in C++:
- Performance: This header contains a large number of common functions that reduce memory allocation and increase code performance.
- Error handling: It returns the fromcharsresult, which indicates if the conversation was successful or not.
- Flexibility: It can handle a large variety of input types and assures the code's adaptability.