Changeset 1403 for trunk


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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1400 r1403  
    214214  suite.add(vec2==vec);
    215215 
     216  std::vector<int> days;
     217  days.resize(m.size());
     218  std::copy(utility::pair_second_iterator(m.begin()),
     219            utility::pair_second_iterator(m.end()),
     220            days.begin());
     221  std::vector<int> days_correct;
     222  days_correct.push_back(30);
     223  days_correct.push_back(31);
     224  days_correct.push_back(30);
     225  days_correct.push_back(30);
     226  for (size_t i=0; i<days.size(); ++i)
     227    if (!suite.add(days[i]==days_correct[i]))
     228      suite.err() << "Error: days[" << i << "] = " << days[i]
     229                  << " expected " << days_correct[i] << "\n";
     230  days = std::vector<int>(days.size(), 0);
     231  std::copy(days.begin(), days.end(), utility::pair_second_iterator(m.begin()));
     232  for (std::map<std::string, int>::const_iterator i=m.begin(); i!=m.end(); ++i)
     233    if (!suite.add(i->second==0) )
     234      suite.err() << "Error: m[" << i->first << "] = " << i->second
     235                  << " expected 0\n";
    216236
    217237  if (ok_cached && !suite.ok())
  • 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.