Use of for_each on map elements

前端 未结 11 745
北荒
北荒 2021-01-30 10:29

I have a map where I\'d like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associativ

11条回答
  •  心在旅途
    2021-01-30 11:05

    I wrote this awhile back to do just what you're looking for.

    namespace STLHelpers
    {
        //
        // iterator helper type for iterating through the *values* of key/value collections
        //
    
        /////////////////////////////////////////////
        template
        struct _value_iterator
        {
            explicit _value_iterator(typename _traits::iterator_type _it)
                : it(_it)
            {
            }
    
            _value_iterator(const _value_iterator &_other)
                : it(_other.it)
            {
            }
    
            friend bool operator==(const _value_iterator &lhs, const _value_iterator &rhs)
            {
                return lhs.it == rhs.it;
            }
    
            friend bool operator!=(const _value_iterator &lhs, const _value_iterator &rhs)
            {
                return !(lhs == rhs);
            }
    
            _value_iterator &operator++()
            {
                ++it;
                return *this;
            }
    
            _value_iterator operator++(int)
            {
                _value_iterator t(*this);
                ++*this;
                return t;
            }
    
            typename _traits::value_type &operator->()
            {
                return **this;
            }
    
            typename _traits::value_type &operator*()
            {
                return it->second;
            }
    
            typename _traits::iterator_type it;
        };
    
        template
        struct _map_iterator_traits
        {
            typedef typename _tyMap::iterator iterator_type;
            typedef typename _tyMap::mapped_type value_type;
        };
    
        template
        struct _const_map_iterator_traits
        {
            typedef typename _tyMap::const_iterator iterator_type;
            typedef const typename _tyMap::mapped_type value_type;
        };
    }
    

提交回复
热议问题