1. Converting between wide and narrow character strings
#include <codecvt>voidconvert_wide_to_narrow(){std::wstring wstr =L"Hello, world!";// Wide-character stringstd::codecvt_utf8<wchar_t> utf8_conv;// UTF-8 codecvt facetstd::mbstate_t mbs;// Multibyte state// Convert the wide-character string to a narrow-character stringsize_t size =utf8_conv.max_length(wstr.size());// Get the maximum number of bytes requiredchar*str =newchar[size];// Allocate memory for the narrow-character stringauto res =utf8_conv.out(mbs,wstr.data(),wstr.data()+wstr.size(), str, str + size, mbs);if(res ==std::codecvt_base::ok){// Conversion successfulstd::cout << str <<std::endl;// Output the narrow-character string}else{// Conversion failedstd::cerr <<"Conversion failed"<<std::endl;}delete[] str;// Free the memory allocated for the narrow-character string}voidconvert_narrow_to_wide(){std::string str ="Hello, world!";// Narrow-character stringstd::codecvt_utf8<wchar_t> utf8_conv;// UTF-8 codecvt facetstd::mbstate_t mbs;// Multibyte state// Convert the narrow-character string to a wide-character stringsize_t size =utf8_conv.max_length(str.size());// Get the maximum number of wide characters requiredwchar_t*wstr =newwchar_t[size];// Allocate memory for the wide-character stringauto res =utf8_conv.in(mbs,str.data(),str.data()+str.size(), wstr, wstr + size, mbs);if(res ==std::codecvt_base::ok){// Conversion successfulstd::wcout << wstr <<std::endl;// Output the wide-character string}else{// Conversion failedstd::cerr <<"Conversion failed"<<std::endl;}delete[] wstr;// Free the memory allocated for the wide-character string}