std::map::erase() - which overload is faster?

2019-05-13 18:31发布

问题:

The map::erase() method has two overloads to remove a single item:

void erase ( iterator position );
size_type erase ( const key_type& x );

I need to check which version is likely to be faster - my guess would be the first, because the second probably has to call map::find() to look up the iterator?

Can anyone confirm?

Thanks!

回答1:

The first one is amortized constant complexity, the second is logarithmic. It is unlikely that the constant term would be large enough to make the first version slower than the second, but I imagine they must be indistinguishable from each other for very small maps.



标签: c++ map