I would like to remove a key from a STL map. However, map.erase() doesn’t do anything.
How would I go about doing this
I would like to remove a key from a STL map. However, map.erase() doesn’t
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends entirely on how you’re calling it but it sounds like you may be using the
first,lastoption. If you are, you need to keep in mind that it erase elements starting atfirst, up to but excludinglast. Provided you follow that rule, iterator-based removal should work fine, either as a single element or a range.If you’re erasing by key, then it should also work, assuming the key is in there of course.
The following sample code shows all three methods in action:
which outputs: