c++ - 为什么 boost locale 不提供字符级规则类型?

标签 c++ c++11 boost

环境:boost1.53.0 c++11;

C++ 新手。

在boost locale边界分析中,规则类型是为word(例如boundary::word_letter, boundary::word_number)和sentence指定的,但是没有字符的边界规则类型。我想要的只是类似于 isUpperCase()、isLowerCase()、isDigit()、isPunctuation()

尝试了 boost 字符串算法,但不起作用。

boost::locale::generator gen;
std::locale loc = gen("ru_RU.UTF-8");
std::string context = "ДВ";
std::cout << boost::algorithm::all(context, boost::algorithm::is_upper(loc));

为什么这些功能在 Java 或 Python 中可以轻松访问,但在 C++ 中却如此令人困惑?有什么一致的方法可以实现这些吗?

最佳答案

这对我在 VS 2013 下有效。

locale::global(locale("ru-RU")); 
std::string context = "ДВ"; 
std::cout << any_of(context.begin(), context.end(), boost::algorithm::is_upper());

打印 1

如何初始化语言环境非常重要。

更新:

这是在 Ubuntu 下运行的解决方案。

#include <iostream>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/locale.hpp>

using namespace std;

int main()
{
    locale::global(locale("ru_RU"));

    wstring context = L"ДВ";
    wcout << boolalpha << any_of(context.begin(), context.end(), boost::algorithm::is_upper());

    wcout<<endl;

    wstring context1 = L"ПРИВЕТ, МИР"; //HELLO WORLD in russian
    wcout << boolalpha << any_of(context1.begin(), context1.end(), boost::algorithm::is_upper());

    wcout<<endl;

    wstring context2 = L"привет мир"; //hello world in russian
    wcout << boolalpha << any_of(context2.begin(), context2.end(), boost::algorithm::is_upper());

    return 0;
}

打印

true
true
false

这也适用于 boost::algorithm::all。

wstring context = L"ДВ";
wcout << boolalpha << boost::algorithm::all(context, boost::algorithm::is_upper());

关于c++ - 为什么 boost locale 不提供字符级规则类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27682789/

相关文章:

c++ - 检索 OpenGL GLSL 中的绘制调用数

c++ - 如何从文本文件中获取长度为 9 的单词?

c++ - C++ 模板的部分特化 : template parameter not deducible

使用字符串作为打开文件路径的 C++ ifstream 错误

c++ - shared_ptr 作为类成员

windows - 在 Windows 7 上使用 MinGW 编译 boost 之前如何构建 Boost.Build?

c++ - 通过boost从流中读取xml时出错

c++ - 使用 lambda 函数处理 C++ libsigc++ 信号

c++ - 如何在 lambda 表达式中捕获单个类数据成员?

c++ - 继承对象指针算法的安全性