How to optimize erasing from multimap

Posted by Dominating on Stack Overflow See other posts from Stack Overflow or by Dominating
Published on 2010-03-29T06:13:07Z Indexed on 2010/03/29 6:23 UTC
Read the original article Hit count: 346

Filed under:

I have two multimaps defined so multimap phoneNums; and multimap numPhones; they are some kind of phone registry - phoneNums contains Key name, and second argument phonenumber, numPhones contain Key phonenumber and second is name. I want to optimize erase from both of them when i want to delete string Key form phoneNums, which is also second element in numPhones. When i enter data it is entered in both multimaps so they are actually the same but with swapped first and second when i put it on tests it says that erasing is too slow - N*N and must be only N

cin>>stringToErase;
                    phoneNums.erase(stringToErase);
                    multimap<string, string>::iterator it;
                    multimap<string, string>::iterator tmpr;
                    for(it = numPhones.begin(); it != numPhones.end();it++)
                    {

                        if(it->second == tringToErase)
                        {   
                            tmpr = it;      
                            numPhones.erase(it,tmpr);                           
                        }                       
                    }

© Stack Overflow or respective owner

Related posts about c++