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 implemented in C++17 with the primary objective of improving the intricacy and performance of the code.
Functions in the __PRESERVE_11__ Header:
The charconv library has two functions:
1. to_chars
The to_chars function transforms a numeric value into its corresponding character representation and stores the result in a designated buffer.
Syntax:
It has the following syntax:
to_chars_result to_chars( char* initial, char* final, T val );
Parameters:
Pointers referring to the beginning and conclusion of the buffers where the result will be inscribed.
val: The numeric value to be transformed.
Return Value:
It produces an instance of tocharsresult containing two attributes:
- ptr: a pointer to the initial character after the completion of the characters processing.
- ec: This signifies an error code that indicates the success or failure of the conversion operation. When the conversion is successful, the error code will be assigned as errc. In case of an error, a distinct error code will be assigned.
Example:
This code snippet demonstrates the utilization of the to_chars function from the <charconv> header to transform an integer value into a sequence of characters.
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 function is employed to convert the character representation of a number into its corresponding numeric value.
Parameters:
The
- initial and
- final parameters represent the beginning and conclusion of the character that requires conversion.
The
- value parameter holds the numeric value to be transformed.
Return on Value
It provides the fromcharsresult entity, containing two attributes:
- ptr: This points to the first character after the last successfully converted character.
- ec: This error code signifies the outcome of the conversion process. It is assigned as errc for successful conversions and an appropriate error code for unsuccessful ones.
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 __PRESERVE_13__ 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.