环境: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/