O
olanglois
Hi,
I was asking myself to following question. What is better to erase an
element from a STL map:
calling (option #1)
size_type erase(const key_type& k)
or calling (option #2)
iterator find(const key_type& k) followed by
void erase(iterator pos) ?
I have looked into the STL implementation source code that I am using
and in the first case when calling size_type erase(const key_type& k)
in VC++.NET2003, the following happens:
It calls
equal_range()
which calls lower_bound() and upper_bound()
then compute the distance between iterators returned by equal_range()
then calls void erase(iterator first, iterator last)
which finally calls void erase(iterator pos)
In the second case, find() just calls lower_bound().
artifact and with other STL implementations, option #1 might be better?
Why someone would want to use option #1 except maybe for saving some
typing?
Thank you,
Olivier Langlois
http://www.olivierlanglois.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
I was asking myself to following question. What is better to erase an
element from a STL map:
calling (option #1)
size_type erase(const key_type& k)
or calling (option #2)
iterator find(const key_type& k) followed by
void erase(iterator pos) ?
I have looked into the STL implementation source code that I am using
and in the first case when calling size_type erase(const key_type& k)
in VC++.NET2003, the following happens:
It calls
equal_range()
which calls lower_bound() and upper_bound()
then compute the distance between iterators returned by equal_range()
then calls void erase(iterator first, iterator last)
which finally calls void erase(iterator pos)
In the second case, find() just calls lower_bound().
STL, using option #2 is more efficient. Is this just an implementationFrom my small investigation, it seems that, at least with VC++.NET2003
artifact and with other STL implementations, option #1 might be better?
Why someone would want to use option #1 except maybe for saving some
typing?
Thank you,
Olivier Langlois
http://www.olivierlanglois.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]