boost函数:clamp、gather、hex、is_palindrome

clamp:

template<typename T, typename Pred> 
  T const & clamp ( T const& val, 
    typename boost::mpl::identity<T>::type const & lo, 
    typename boost::mpl::identity<T>::type const & hi, Pred p )
  {
//    assert ( !p ( hi, lo ));    // Can't assert p ( lo, hi ) b/c they might be equal
    return p ( val, lo ) ? lo : p ( hi, val ) ? hi : val;
  } 

简述:给定三个数,返回中间数(Pred默认使用std::less)。

示例:

    int foo = 23;
    foo = boost::algorithm::clamp(foo, 1, 10);

    cout << foo << endl;

gather:

template <
    typename BidirectionalIterator,  // Iter models BidirectionalIterator
    typename Pred>                   // Pred models UnaryPredicate
std::pair<BidirectionalIterator, BidirectionalIterator> gather 
        ( BidirectionalIterator first, BidirectionalIterator last, BidirectionalIterator pivot, Pred pred )
{
//  The first call partitions everything up to (but not including) the pivot element,
//  while the second call partitions the rest of the sequence.
    return std::make_pair (
        std::stable_partition ( first, pivot, !boost::bind<bool> ( pred, _1 )),
        std::stable_partition ( pivot, last,   boost::bind<bool> ( pred, _1 )));
}

简述:将容器中满足pred函数要求的元素聚集到pivot的两边,并返回满足元素的区间迭代器。


stable_partition函数作用是依据pred函数将容器分为两部分(前一部分满足函数要求,后一部分不满足函数要求),且元素的相对位置不发生变化。


示例:

int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    auto range = boost::algorithm::gather(arr, arr + 10, arr + 4, isEven);

    //1	3 0 2 4 6 8 5 7 9
    for(auto i = 0; i < sizeof(arr) / sizeof(int); ++i)
    {
        cout << *(arr + i) << " ";
    }
    cout << endl;

    //0	2 4 6 8
    for(auto ite = range.first; ite != range.second; ++ite)
    {
        cout << *ite << " ";
    }
    cout << endl;


hex:



is_palindrome:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值