// Example code to convert between UTF-8 and UTF-16 in C++
#include
#include
#include
#include
std::string utf16_to_utf8(const std::u16string& utf16) {
std::wstring_convert<:codecvt_utf8_utf16>, char16_t> converter;
return converter.to_bytes(utf16);
}
std::u16string utf8_to_utf16(const std::string& utf8) {
std::wstring_convert<:codecvt_utf8_utf16>, char16_t> converter;
return converter.from_bytes(utf8);
}
int main() {
std::u16string utf16 = u"Hello, world!";
std::string utf8 = utf16_to_utf8(utf16);
std::cout << "UTF-8: " << utf8 << std::endl;
std::u16string convertedBack = utf8_to_utf16(utf8);
std::wcout << L"UTF-16: " << convertedBack << std::endl;
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?