我编写了这个函数来使字符串全部大写。不幸的是,它不适用于扩展的 ASCII 字符,如 ä、ö、ü、é、è 等。如何将字符串转换为大写并同时转换这些字符(Ä、Ö、Ü、É、È)?
void toUppercase1(string & strInOut)
{
std::locale loc;
for (std::string::size_type i=0; i<strInput.length(); ++i)
{
strInput.at(i) = std::toupper(strInput.at(i),loc);
}
return;
}
这是新版本。
#include <clocale>
void toUppercase2(string& strInput)
{
setlocale(LC_CTYPE, "sv_SE");
for (std::string::size_type i=0; i<strInput.length(); ++i) {
strInput.at(i) = toupper(strInput.at(i));
}
// reset locale
setlocale(LC_ALL, "");
return;
}
不幸的是,上面的方法 toUppercase2(string&) 非常慢。似乎在全球范围内更改语言环境需要付出一些努力。我假设使用 C++ 中的语言环境对象,它被初始化一次然后由 toupper 使用要快得多,但我找不到如何正确使用它的示例。
任何关于我可以在哪里找到有关语言环境及其使用的更多信息的提示都表示赞赏。