[Cs3] Class question, erase in maps by key value(s)
Mikhail Nesterenko
mikhail at cs.kent.edu
Wed Feb 16 12:12:44 EST 2022
> so for a given map could you erase by range not by iterators but by
> key values. ex: employees. erase(111 ,555) to remove all employees
> with 111 < Key < 555 or with an iterator to a key value.
There is a solution to this question using lower_bound() and upper_bound()
map::lower_bound(X) returns an iterator to the element with a key no
greater than X
map::upper_bound(X) returns an iterator to the element with a key
(strictly) greater than X
Note the asymmetry in the operation of the two functions: they are
designed to obtain a half-open range.
I came up with example usage:
http://www.cs.kent.edu/~mikhail/classes/cs3/Examples/STLContainersAssociative/mapRangeErase.cpp
thanks,
--
Mikhail
More information about the cs3
mailing list