I recently bumped into string literals and found some new strings like u16string and u32string. I found that wstring can be printed to the console using std::wcout, but that doesn't work for u16string or u32string. How can i print these to the Console??
I guess the below code would work, but note that <codecvt>
is deprecated in c++17.
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
int main() {
std::u16string str = u"sample";
std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
std::cout << converter.to_bytes(str) << std::endl;
return 0;
}
Maybe something like this could work as well,
#include <string>
#include <iostream>
int main() {
std::u16string str(u"abcdefg");
for (const auto& c: str)
std::cout << static_cast<char>(c);
}
Not sure how robust the latter is, and how efficient you need it to be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With