Changeset 1403 for trunk/yat/utility


Ignore:
Timestamp:
Aug 7, 2008, 5:00:49 PM (13 years ago)
Author:
Peter
Message:

function pair_second_iteratr that takes an iterator with value std::pair and transform it into an iterator of type pair::second_type. refs #369

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/stl_utility.h

    r1399 r1403  
    306306
    307307  /**
     308     \brief Functor that return std::pair.second
     309
     310     \see pair_second_iterator
     311
     312     \since New in yat 0.5
     313   */
     314  template <class Pair>
     315  struct PairSecond
     316  {
     317    /**
     318       The type returned is Pair::second_type& with the exception when
     319       Pair is const and Pair::second_type is non-const, in which case
     320       const Pair::first_type& is return type.
     321     */
     322    typedef typename boost::mpl::if_<
     323                  typename boost::is_const<Pair>::type,
     324                  typename boost::add_const<typename Pair::second_type>::type&,
     325                  typename Pair::second_type&>::type result_type;
     326   
     327    /**
     328       The argument type is Pair&.
     329     */
     330    typedef Pair& argument_type;
     331
     332    /**
     333       \return p.first
     334     */
     335    inline result_type operator()(argument_type p) const
     336    { return p.second; }
     337
     338  };
     339
     340
     341  /**
    308342     Creates a transform_iterator that transforms an iterator with
    309343     value type std::pair to an iterator with value type
     
    332366
    333367
     368  /**
     369     Creates a transform_iterator that transforms an iterator with
     370     value type std::pair to an iterator with value type
     371     std::pair::second_type. This can be used, for example, to
     372     communicate between a std::map and std::vector
     373
     374     \code
     375     std::map<std::string, int> map;
     376     ...
     377     std::vector<int> vec(map.size(),0);
     378     std::copy(vec.begin(), vec.end(), pair_second_iterator(map.begin()));
     379     \endcode
     380
     381     \since New in yat 0.5
     382   */
     383  template<class Iter>
     384  boost::transform_iterator<
     385    PairSecond<typename std::iterator_traits<Iter>::value_type>,
     386    Iter> pair_second_iterator(Iter i)
     387  {
     388    typedef PairSecond<typename std::iterator_traits<Iter>::value_type> PF;
     389    return boost::transform_iterator<PF, Iter>(i, PF());
     390  }
     391
     392
    334393
    335394
Note: See TracChangeset for help on using the changeset viewer.