C++Primer模板

  1. find 算法模板
    template<typename Iterator, typename Value>
    auto find(Iterator first, Iterator last, Value const& value)
    {
        for (; first != last && *first != value; ++first);
        return first;
    }
  • print 函数模板
template<typename Arr>
void print(Arr const& arr)
{
    for (auto const& elem : arr)
        std::cout << elem << std::endl;
}

  • 接受数组对象的begin 和 end 迭代器
template<typename T, unsigned size>
T* begin_def(T(&arr)[size])
{
    return arr;
}


// the same as std::end
template<typename T, unsigned size>
T* end_def(T (&arr)[size])
     ^^//We usually don't use a function name which is the same as the function of standard libary
       ^^ //This should not be const
{
    return arr + size;
}
  • 返回数组大小的模板
template<typename T, unsigned size>
constexpr unsigned getSize(const T(&)[size])
{
    return size;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值