why doesn't erase method for map take key value as its parameter?

Y

yaru22

When I look at the C++ reference, erase method for map takes an
iterator as a parameter. I'm wondering why it takes iterator instead
of key value. Wouldn't it be faster to take key value and erase the
object? (because lookup operation using key value would be faster than
creating an iterator and iterate through the map?)
 
J

John Harrison

yaru22 said:
When I look at the C++ reference, erase method for map takes an
iterator as a parameter. I'm wondering why it takes iterator instead
of key value. Wouldn't it be faster to take key value and erase the
object? (because lookup operation using key value would be faster than
creating an iterator and iterate through the map?)

When you look up a key, you are getting the iterator for that key. So
obviously deleteing using an iterator must be faster than looking to the
key to get the iterator and then using the iterator to delete.

john
 
V

Victor Bazarov

yaru22 said:
When I look at the C++ reference, erase method for map takes an
iterator as a parameter. I'm wondering why it takes iterator instead
of key value. Wouldn't it be faster to take key value and erase the
object? (because lookup operation using key value would be faster than
creating an iterator and iterate through the map?)

Sorry... What reference are you looking at that doesn't have the 'erase'
member of 'std::map' that takes a key value?

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top