我在C++中遇到一个字符串问题,其中有几个西班牙语单词。这意味着我有很多带重音符号和波浪符号的单词。我想用它们没有重音的对应词替换它们。例如:我想将此单词“había”替换为habia。我尝试直接使用string类的replace方法进行替换,但无法使其正常工作。
我正在使用以下代码:
for (it= dictionary.begin(); it != dictionary.end(); it++)
{
strMine=(it->first);
found=toReplace.find_first_of(strMine);
while (found!=std::string::npos)
{
strAux=(it->second);
toReplace.erase(found,strMine.length());
toReplace.insert(found,strAux);
found=toReplace.find_first_of(strMine,found+1);
}
}
其中dictionary
是一个类似于下面这样的映射(有更多的条目):
dictionary.insert ( std::pair<std::string,std::string>("á","a") );
dictionary.insert ( std::pair<std::string,std::string>("é","e") );
dictionary.insert ( std::pair<std::string,std::string>("í","i") );
dictionary.insert ( std::pair<std::string,std::string>("ó","o") );
dictionary.insert ( std::pair<std::string,std::string>("ú","u") );
dictionary.insert ( std::pair<std::string,std::string>("ñ","n") );
要替换的字符串是toReplace
:
std::string toReplace="á-é-í-ó-ú-ñ-á-é-í-ó-ú-ñ";
我显然缺少了什么,我搞不清楚它是什么。 是否有任何库可供使用?
谢谢。