函数latin2cyr,反之亦然,在c ++中

时间:2012-04-12 10:24:37

标签: c++ string stl

使用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容器,需要帮助!

2 个答案:

答案 0 :(得分:0)

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

map letters = 
 {{ "f", "ф"},
  { "h", "х"},
  //and so on
 };

答案 1 :(得分: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;
}

任何优化它的想法?