123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include <set>
- template<
- class Key,
- class Compare = std::less<Key>,
- class Allocator = std::allocator<Key>
- > class multiset : public std::multiset<Key, Compare, Allocator>
- {
- typedef multiset<Key, Compare, Allocator> self;
- typedef std::multiset<Key, Compare, Allocator> parent;
- public:
- typedef typename parent::iterator iterator;
- typedef typename parent::const_iterator const_iterator;
- // returns iterator poiting to first element equal to key.
- const_iterator equal( const Key& key ) const
- {
- const_iterator eq = self::lower_bound(key);
- if(eq == self::end() || *eq != key) {
- return self::end();
- } else {
- return eq;
- }
- }
- // returns iterator poiting to first element equal to key.
- // if no such element exists, return iterator pointing to last element less than key.
- const_iterator equal_or_less( const Key& key ) const
- {
- if(self::size() == 0) {
- return self::end();
- } else {
- const_iterator equal_or_greater = self::lower_bound(key);
- if(equal_or_greater == self::end()) {
- // no element is equal or greater, so take the last
- return --self::end();
- } else if(*equal_or_greater == key) {
- return equal_or_greater;
- } else if(equal_or_greater == self::begin()) {
- return self::end();
- } else {
- return --equal_or_greater;
- }
- }
- }
- const_iterator equal_or_less_or_max( const Key& key ) const
- {
- const_iterator eq_or_less = self::equal_or_less(key);
- if(eq_or_less == self::end()) {
- return self::max();
- } else {
- return eq_or_less;
- }
- }
- const_iterator greater_or_min( const Key& key ) const
- {
- const_iterator greater = self::upper_bound(key);
- if(greater == self::end()) {
- return min();
- } else {
- return greater;
- }
- }
- // returns an iterator pointing to the last element that is less than key.
- const_iterator less( const Key& key ) const
- {
- const_iterator lower = self::lower_bound(key);
- if(lower == self::begin()) {
- return self::end();
- } else {
- lower--;
- return lower;
- }
- }
- const_iterator less_or_max( const Key& key ) const
- {
- const_iterator less = self::less(key);
- if(less == self::end()) {
- return self::max();
- } else {
- return less;
- }
- }
- const_iterator max() const
- {
- const_iterator max = self::end();
- if(self::size() > 0) {
- max--;
- }
- return max;
- }
- const_iterator min() const
- {
- return self::begin();
- }
- };
|