我试图迭代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++ …