【C++ Primer】【学习笔记】【第十六章】模板与泛型编程之一:模板定义

一、函数模板
格式:
template <typename T>
int compare(const T &v1, const T &v2)
{
    ........
}
注1:模板形参表不能为空。
注2: typename也可以写成class,含义相同。

二、内联函数模板
格式:
template <typename T>  inline
int compare(const T &v1, const T &v2)
{
    ........
}
注:inline关键字必须放在模板形参表之后,返回类型之前;不能放在template关键字之前。

三、类模板
格式:
template <class T>
class Queue
{
public:
    Queue();
    T &front();
    void push(const T &);
    void pop();
private:
    //...
}
使用类模板时,必须为模板形参显式指定实参,如:
Queue<int> qi;

四、模板声明
像其他任意的类或者函数一样,模板可以只声明而不定义。如:
template <typename T> int compare(const T&, const T&);

注:同一模板的声明和定义中,模板形参的名字不必相同。如下两种声明表示同一个模板:
template <typename T> int compare(const T&, const T&);
template <typename U> int compare(const U&, const U&);

五、其他注意事项
1、在模板定义内部使用类型的话,需要在类型之前加上typename以显示说明是类型而不是数据,否则编译器会认为其为数据而不是类型;
2、在编写模板代码时,对实参类型要尽可能地少;
3、编写泛型代码的两个重要原则:
      - 模板的形参加上const
      - 函数体中的测试只用<进行比较


习题16.1:模板函数求绝对值


#include <iostream>

using namespace std;

template <typename T> T abs(const T v1)
{
        return (v1 > 0) ? v1 : (0 - v1);
}

int main()
{
        cout << "abs of -10 is: " << abs(-10) << endl;
        cout << "abs of -10.2 is: " << abs(-10.2) << endl;

        return 0;
}
[chapter16]$ ./a.out
abs of -10 is: 10
abs of -10.2 is: 10.2


习题16.9:find算法模板

#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename InIt, typename T>
InIt findElem(InIt first, InIt last, const T& val)
{
        while (first != last)
        {
                if (*first == val)
                {
                        return first;
                }
                ++first;
        }
        return last;
}

int main()
{
        int ia[] = {1, 2, 3, 4, 5, 6, 7};
        string sa[] = {"this", "is", "Mary", "test", "example"};
        vector<int> ivec(ia, ia + 7);
        vector<string> svec(sa, sa + 5);

        vector<int>::iterator iit;
        if ((iit = findElem(ivec.begin(), ivec.end(), 6)) != ivec.end())
        {
                cout << "found this element: " << *iit << endl;
        }
        else
        {
                cout << "no such element" << endl;
        }

        vector<string>::iterator sit;
        if ((sit = findElem(svec.begin(), svec.end(), "Mary")) != svec.end())
        {
                cout << "found this element: " << *sit << endl;
        }
        else
        {
                cout << "no such element" << endl;
        }

        return 0;
}
[chapter16]$ ./a.out 
found this element: 6 
found this element: Mary



习题16.13:使用模板打印容器元素

#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename Param>
void print(const Param& c)
{
        typename Param::size_type index = 0;
        while (index != c.size())
        {
                cout << c[index] << ' ';
                ++index;
        }
}
int main()
{
        int ia[] = {1, 2, 1, 4, 1, 6, 1};
        string sa[] = {"this", "is", "Mary", "test", "example"};
        vector<int> ivec(ia, ia + 7);
        vector<string> svec(sa, sa + 5);
        print(ivec);
        cout << endl;
        print(svec);
        cout << endl;
        return 0;
}
[chapter16]$ ./a.out 
1 2 1 4 1 6 1 
this is Mary test example



习题16.14:使用模板打印容器元素(使用迭代器进行处理)

#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename Param>
void print(const Param& c)
{
        typename Param::const_iterator iter;
        for (iter = c.begin(); iter != c.end(); ++iter)
        {
                cout << *iter << ' ';
        }

        return;
}

int main()
{
        int ia[] = {1, 2, 1, 4, 1, 6, 1};
        string sa[] = {"this", "is", "Mary", "test", "example"};
        vector<int> ivec(ia, ia + 7);
        vector<string> svec(sa, sa + 5);

        print(ivec);
        cout << endl;

        print(svec);
        cout << endl;

        return 0;
}
[chapter16]$ ./a.out 
1 2 1 4 1 6 1 
this is Mary test example



习题16.15:编写可以确定数组长度的函数模板


#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T, std::size_t N>
std::size_t size(T (&arr)[N])
{
        return N;
}

int main()
{
        int ia[] = {1, 2, 1, 4, 1, 6, 1};
        cout << "the length of array ia[] is: " << size(ia) << endl;
        return 0;
}
[chapter16]$ ./a.out 
the length of array ia[] is: 7


习题16.16:编写可以打印不同长度数组的内容的函数模板

#include <iostream>
#include <string>
#include <vector>
using namespace std;

template <typename T, std::size_t N>
void printValue(T (&arr)[N])
{
        for (std::size_t i = 0; i != N; ++i)
        {
                std::cout << arr[i] << ' ';
        }
        cout << endl;
        return;
}

int main()
{
        int ia[] = {1, 2, 1, 4, 1, 6, 1};
        printValue(ia);

        string sa[] = {"this", "is", "Mary", "test", "example"};
        printValue(sa);

        return 0;
}
[chapter16]$ ./a.out 
1 2 1 4 1 6 1 
this is Mary test example


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值