开发者

std::map::erase infinite loop

I have a map of a vector of char's and a vector of strings. Every so often, if I've seen the vector of characters before, I'd like to add a string to my vector of strings. Below is my code to do that.

map<vector<char>, vector<string>>::iterator myIter = mMyMap.find(vChars);
if(myIter != mMyMap.end()) {
    vector<string> vStrings = myIter->second;
    mMyMap.erase(myIter);
    vStrings.push_back(some_other_string);
    mMyMap.insert(pair<vector<char>, vector<string>>(vChars, vStrings));
    return TRUE;
}

The call to mMyMap.erase() seems to get stuck an in infinite loop though. I'm guessing it's because vStrings isn'开发者_如何学Got getting a deep-copy of myIter->second.

Do I need to initalize vStrings like:

vector<string> vStrings(myIter->second);

Or what's the proper fix?


I don't see an error in the posted code fragment (other than a missing )). But may I suggest simplifying lines 2-8 to:

if(myIter != mMyMap.end()) { 
    myIter->second.push_back(some_other_string); 
} 


vector vStrings = myIter->second;
and
vector vStrings(myIter->second);
are same things. They both call copy constructor. And the copy is deep copy only. My guess is that the vector that is getting copied is too big(or long). Each element of the vector will be copied one by one. And hence the time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜