6

我试图了解如何使用 boost::locale 来比较忽略大小写和变体的字符串。我直接尝试了 Boost 文档中的代码:

http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/collat​​ion.html

boost::locale::generator gen;
std::locale vLocale = gen("");


std::wstring a=L"Façade", b=L"facade";

// Following will throw bad_cast
bool eq = std::use_facet<boost::locale::collator<wchar_t>>(vLocale).compare(
    boost::locale::collator_base::secondary,
    a,
    b
) == 0;

if(eq) std::cout << "OK" << std::endl;

此代码在运行时会抛出 std::bad_cast 异常。我在 boost::locale::generator 的构造函数中尝试了很多参数。有谁知道我遇到的问题?

我正在使用带有 g++4.6 和 Boost 1.51.0 的 C++11

4

1 回答 1

5

您似乎使用了不正确的语言环境对象。首先,您应该使用全局语言环境,然后(如果您想使用std::cout)将语言环境灌输到流中。像这样的东西:

boost::locale::generator gen;
std::locale loc = gen("");
std::locale::global(loc);

但是,在您的示例中,如果您不使用std::cout,只需设置全局语言环境,以便您准备好所需的方面。

于 2012-10-29T22:17:59.970 回答