fork(8) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cwchar>
  5. #include <clocale>
  6. int main()
  7. {
  8. std::string s="йаᠭ1";
  9. std::vector<std::string> letters;
  10.  
  11. std::setlocale(LC_ALL, ""); // or "en_US.utf8", or any other .utf8
  12. std::mbstate_t state = std::mbstate_t(); // initial state
  13. const char* ptr = s.c_str();
  14. const char* end = s.c_str() + s.size();
  15. int len;
  16. wchar_t wc;
  17. while((len = std::mbrtowc(&wc, ptr, end-ptr, &state)) > 0) {
  18. letters.push_back(std::string(ptr, ptr+len));
  19. ptr += len;
  20. }
  21.  
  22. for(size_t n = 0; n < letters.size(); ++n)
  23. std::cout << "The size of the letter " << letters[n] << " in UTF-8 is " << letters[n].size() << '\n';
  24. }
  25.  
Success #stdin #stdout 0s 4560KB
stdin
Standard input is empty
stdout
The size of the letter й in UTF-8 is 2
The size of the letter а in UTF-8 is 2
The size of the letter ᠭ in UTF-8 is 3
The size of the letter 1 in UTF-8 is 1