0

使用 C++ STL 将 AnsiString 转换为 Cyrillic 的最简单/最佳方法是什么!

在 php 中它会是

<?php
  // функция превода текста с кириллицы в траскрипт
  function encodestring($st)
  {
    // Сначала заменяем "односимвольные" фонемы.
    $st=strtr($st,"абвгдеёзийклмнопрстуфхъыэ_",
    "abvgdeeziyklmnoprstufh'iei");
    $st=strtr($st,"АБВГДЕЁЗИЙКЛМНОПРСТУФХЪЫЭ_",
    "ABVGDEEZIYKLMNOPRSTUFH'IEI");
    // Затем - "многосимвольные".
    $st=strtr($st, 
                    array(
                        "ж"=>"zh", "ц"=>"ts", "ч"=>"ch", "ш"=>"sh", 
                        "щ"=>"shch","ь"=>"", "ю"=>"yu", "я"=>"ya",
                        "Ж"=>"ZH", "Ц"=>"TS", "Ч"=>"CH", "Ш"=>"SH", 
                        "Щ"=>"SHCH","Ь"=>"", "Ю"=>"YU", "Я"=>"YA",
                        "ї"=>"i", "Ї"=>"Yi", "є"=>"ie", "Є"=>"Ye"
                        )
             );
    // Возвращаем результат.
    return $st;
  }
?>

我更喜欢使用 STL Containers,需要帮助!

4

2 回答 2

0

您也可以在 C++ 中使用相同的代码样式。查看 std::map 容器。在最新的标准 C++11 中,您还可以以非常方便的方式对其进行初始化:

map letters = 
 {{ "f", "ф"},
  { "h", "х"},
  //and so on
 };
于 2012-04-12T11:35:23.363 回答
0

最后我写了我自己的函数,它运行良好!我认为它会完成我的工作,但没有那么优化!试图像这样使用 map.find() smth 但是.....

AnsiString Latin2Cyr(AnsiString inStr)
{
    int l=inStr.Length();
    if (!l) return "";

    AnsiString strLat[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    AnsiString strCyr[]={"а","б","с","д","е","ф","г","х","и","дж","к","л","м","н","о","п","к","р","с","т","у","в","у","кс","й","з"};
    AnsiString outStr="";
    typedef multimap<AnsiString,AnsiString> mmp;
    mmp mStr;
    int ln=(sizeof(strLat))/(sizeof(AnsiString));
    for (int i = 0; i < ln; i++) mStr.insert(mmp::value_type(strLat[i],strCyr[i]));
    AnsiString k=""; mmp::iterator it;
    for (int j = 1; j <l+1; j++) {
        for ( it=mStr.begin(); it!= mStr.end();++it ){
            outStr+=((*it).first==inStr.SubString(j,1))? (*it).second : k ;
        }
    }
    return outStr;
}

有什么优化它的想法吗?

于 2012-04-12T14:34:37.767 回答