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't 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.