使用 Boost.Locale 库检索代码点
Retrieving code points using Boost.Locale library
从给定的Unicode字符串中,我想检索构成该字符串的代码点列表。为此,我从 Boost 的角色迭代示例中复制了以下示例:
#include <boost/locale.hpp>
using namespace boost::locale::boundary;
int main()
{
boost::locale::generator gen;
std::string text = "To be or not to be";
// Create mapping of text for token iterator using global locale.
ssegment_index map(character, text.begin(), text.end(), gen("en_US.UTF-8"));
// Print all "words" -- chunks of word boundary
for (ssegment_index::iterator it = map.begin(), e = map.end(); it != e; ++it) {
std::cout <<"""<< * it << "", ";
}
std::cout << std::endl;
return 0;
}
它返回我的字符(根据 Boost 的文档与代码点不同),如下所示:
"T", "o", " ", "b", "e", " ", "o", "r", " ", "n", "o", "t", " ", "t", "o", " ", "b", "e",
我读到使用 boost::locale::util::base_converter 类中的 to_unicode
函数可以检索给定字符串的代码点。但我不确定如何。我尝试了以下代码,但没有帮助:
for (ssegment_index::iterator it = map.begin(), e = map.end(); it != e; ++it) {
std::cout << """ << * it << "", ";
boost::locale::util::base_converter encoder_decoder;
virtual uint32_t test1 = encoder_decoder.to_unicode(it->begin(), it->end() );
}
它返回"类型不匹配"错误。我认为to_unicode()
函数的参数一定不同
我正在考虑仅使用 Boost 来检索代码点,而不是像这里或这里这样的现有解决方案,因为 Boost 提供了许多有用的函数来识别各种语言的换行符、断字符等。
要获取代码点,您可以使用boost::u8_to_u32_iterator
。这是因为 UTF-32 字符等于其代码点。
#include <boost/regex/pending/unicode_iterator.hpp>
#include <string>
#include <iostream>
void printCodepoints(std::string input) {
for(boost::u8_to_u32_iterator<std::string::iterator> it(input.begin()), end(input.end()); it!=end; ++it)
std::cout <<"""<< * it << "", ";
}
int main() {
printCodepoints("Hello World!");
return 0;
}
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何从C++代码中检索 QML 的文本字段中的文本?
- 从C++(非托管代码)检索数组到 C 尖锐形式(托管)
- 我正在编写一个代码来将 int 数组存储在文件中,然后用 c++ 检索它,但是检索第一项是假值,我该如何解决这个问题?
- C MACOS-编程检索代码符号证书信息
- JIT代码生成后,如何检索LLVM ::指令地址
- 从德州仪器 C200 DSP 检索代码
- 用于在C++代码库中检索函数和方法列表的工具
- 如何检索连接到服务器的硬盘信息?示例 C/C 代码
- 使用 Boost.Locale 库检索代码点
- gurobi - 错误代码 = 10004 无法检索属性'X'
- 无法从C++代码中检索正确的字符串.是否有一种通用类型用于处理c#上的无符号字符*
- 安卓系统上的JNI:如何从Java代码中检索字符串
- 这个代码如何从两个2D向量的叉积中检索2D向量
- 使用step方法检索图像的bgr值的代码有什么问题
- 是否有可能将复制粘贴的代码跟踪到从中检索的网站