I have a requirement in cpp where we need to search some pattern in incoming string and need to replace with corresponding values.Here comes the tricky part incoming string can contain special characters(like ° etc) and pattern can be single character or group of characters. Initially thought to store pattern string and replacement value in Map but i am facing problems with special characters please let me know the correct approach to solve this problem.
example: ° need to be replace by "degrees"
int main() {
map<string,string> tempMap;
pair<string,string> tempPair;
tempMap.insert(pair<string,string>("°","degrees"));
tempMap.insert(pair<string,string>("one","two"));
tempMap.insert(pair<string,string>("three","four"));
typedef map<string,string>::iterator it_type;
string temp="abc°def";
for(it_type iterator = tempMap.begin(); iterator != tempMap.end(); iterator++)
{
//cout << iterator->first << " " << iterator->second << endl;
string::size_type found=temp.find(iterator->first);
if (found!=string::npos)
{
temp.replace(found,1,iterator->second);
cout << endl <<"after replacement " << temp;
}
}
}
output : after replacement abcdegrees�def
in output getting specialcharacter, this is because special character ° occupying 2 bytes.