Why C++ std::string can support characters in Japanese, French?

Viewed 811

Previously I thought that C++ std::string can only support ASCII character sets. For other character sets for example, French, Japanese characters, encoding for example UTF-8/UTF-16 will be needed.

I just try below code and it seems that C++ std::string has already support Japanese characters and French. Guess also support all other languages.

How can it happen? Does it mean that we just need std::string to handle all human languages?

string s;

s = "今年1年の世相を4字で振り返る恒例の「創作四字熟語」の優秀・入選50作品を発表した";

string t;

t = "Vélo, sac, appareil photo: bleu en un «Klein» d'œil pour Noël";

cout<<s<<'\n';

cout<<t<<'\n';

Output:

今年1年の世相を4字で振り返る恒例の「創作四字熟語」の優秀・入選50作品を発表した

Vélo, sac, appareil photo: bleu en un «Klein» d'œil pour Noël

1 Answers

A std::string can support an arbitrary byte stream, including UTF-8, which is what you're seeing here. On the input side, your compiler evidently supports it, and on the output side your terminal program does.

Where things might break down is if you assume in your code that one char in your std:: string corresponds to one character on the screen. That is not true for UTF-8, as you probably already know.

Related