C++ 如何解析UTF-8中文字符串
我正在尝试解析可能包含中文字符的C++ 如何解析UTF-8中文字符串,c++,boost,utf-8,boost-spirit,C++,Boost,Utf 8,Boost Spirit,我正在尝试解析可能包含中文字符的std::string。例如,字符串包含 哈囉hi你好hello 我想把它们分成6个字符串:哈,囉,hi,你,好,你好。现在,通过使用getline()从文本文件中获取字符串。参考本文,以下是我当前的代码: #include <boost/regex/pending/unicode_iterator.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/range.h
std::string
。例如,字符串包含
哈囉hi你好hello
我想把它们分成6个字符串:哈代码>,囉代码>,hi
,你代码>,好代码>,你好
。现在,通过使用getline()
从文本文件中获取字符串。参考本文,以下是我当前的代码:
#include <boost/regex/pending/unicode_iterator.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/range.hpp>
#include <iterator>
#include <iostream>
#include <ostream>
#include <cstdint>
#include <string>
using namespace boost;
using namespace std;
using namespace std::string_literals;
int main()
{
string str = u8"哈囉hi你好hello"; //actually got from getline()
auto &&utf8_text = str;
u8_to_u32_iterator<const char*>
tbegin(begin(utf8_text)), tend(end(utf8_text));
vector<uint32_t> result;
spirit::qi::parse(tbegin, tend, *spirit::standard_wide::char_, result);
for(auto &&code_point : result) {
cout << code_point << ";";
}
}
但仍然出现错误:调用“begin”和“end”时没有匹配的函数。auto
和字符串文本会导致字符指针。如果你想要std::string
,你必须把它写出来。你试过u8到u32迭代器吗?不能保证begin()
和end()
返回指针。谢谢。现在可以了。
auto str = u8"你好,世界!";
auto &&utf8_text = str;