C++ STL key/value pairs strangely refuse to be eased!!
I write a (bio-tool) program that creates STL key/value pairs like these for example:
"AGA"= 3 "URG" = 5 "ABCD" = 1 "TRD" = 1 Now I am only interested in the pairs with values above 2, so I wrote the following code to remove the ones below: typedef map<string,int> StringIntMap; StringIntMap ph; StringIntMap::iterator pos; for (pos=ph.begin();pos!=ph.end();++pos) { if (pos->second<3) {ph.erase(pos);} } The majority (thousends) of pairs with values below 3 get deleted, but still some pairs with values of 1 remain!! I do not understand, because the code above should logically delete them all! Does anybody have an idea why the could escape deletion? |
erase() on a map will not invalidate iterators EXCEPT the one that points the item being erased, so you need to separate the 'iterating' iterator from the 'erasing' iterator.
Code:
for (map_type::iterator iter=the_map.begin(); iter!=the_map.end(); ) p.s. pop your code samples between code tags like this [ code ] YOUR CODE [ /code ] with no spaces in the brackets - it makes it easier to read :D |
thanks, but it didn't help..
whats the difference between x++ and ++x again? another strange thing is that the way I am outputing the data should never even show the key/value pairs with values lower that 5, according to this code: Code:
typedef map<string,int> StringIntMap; Value / Key ========== 3x AL 3x CR 3x GA 3x GC 3x GF 3x LL 3x LR 3x PH 3x PV 3x RE 3x RV 3x TG 3x VL 3x VP 2x LA 2x RL 1x RRR |
Code:
// ++x does something like this: y = ++x; and y = x++; are different. but I am not sure exactly what is 'officially' mandated wrt implementation. I am slightly surprised this is not working - I would generally look at my own code before searching for an error elsewhere - but this technique is often suggested on places like comp.lang.c++ where contributors tend to be somewhat pedantic to say the least! I am not clear what the purpose of this line is (wrt output): Code:
pos->second/=pos->first.size(); Here : 1x RRR key: RRR had a value: 5, no? similarly, LA and RL had values of 5 and the rest had values of 6 or 7. ... or have I missed something? |
yes, you're right about that.
AFTER I want to delete the key with value smaller than 3, I multiply the value by the length of the key, and revert this by dividing before the output. I do this, because I want to sort the key/value pairs that way. So when I filter out the value=1 pairs after the dividing, I don't get them outputted, as expected. Sorry, this was my mistake! But the key/value pairs with value=1 STILL EXIST, even though I deleted them with your method. I mean because the multiplication is afterwards. This is the whole code I am talking about: Code:
int main(int argc, char *argv[]) { |
I tried to compile your code, using globals:
Code:
typedef map<string,int> StringIntMap; Why not put a check in hash_clean after the first loop if you think the error is there? Code:
for (StringIntMap::iterator iter=ph.begin(); iter!=ph.end(); ++iter) |
All times are GMT -5. The time now is 01:24 PM. |