小编Jan*_*mek的帖子

在C++ 11中迭代UTF-8字符串

我试图迭代UTF-8字符串.我理解的问题是UTF-8字符具有可变长度,所以我不能只迭代char-by-char但我必须使用某种转换.我确信在现代C++中有一个功能,但我不知道它是什么.

#include <iostream>
#include <string>

int main()
{
  std::string text = u8"?abcd?";
  std::cout << text << std::endl; // Prints fine
  std::cout << "First letter is: " << text.at(0) << text.at(1) << std::endl; // Again fine. So '?' is a 2 byte letter?

  for(auto it = text.begin(); it < text.end(); it++)
  {
    // Obviously wrong. Outputs only ascii part of the text (a, b, c, d) correctly
    std::cout << "Iterating: " << *it << std::endl; 
  }
}
Run Code Online (Sandbox Code Playgroud)

编译 clang++ -std=c++11 -stdlib=libc++ …

unicode utf-8 c++11

7
推荐指数
1
解决办法
3736
查看次数

标签 统计

c++11 ×1

unicode ×1

utf-8 ×1