C++primer 16.1.1节练习

练习16.1

调用者调用函数模板时,编译器用函数实参来为我们推断模板实参,用推断出来的模板实参来代替一个特定版本的函数的过程叫做实例化;

练习16.2

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <vector>
 6 #include <set>
 7 
 8 using namespace std;
 9 
10 template<typename T> 
11 int compare(const T& a, const T& b);
12 
13 int main()
14 {
15     int a = 2, b = 3;
16     cout << compare(2, 3) << endl;
17     system("pause");
18     return 0;
19 }
20 
21 template<typename T>
22 int compare(const T & a, const T & b)
23 {
24     if (a < b) return 1;
25     else if (b < a) return -1;
26     return 0;
27 }

练习16.3

如果未重载运算符<,系统会报错,因为这样的一个运算符对于自定义的类类型是未定义的。

练习16.4

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <vector>
 6 #include <list>
 7 
 8 using namespace std;
 9 
10 template<typename T, typename U>
11 T find_value(T beg, T end, U value);
12 
13 int main()
14 {
15     vector<int> vec{ 1,2,3,4,5,6,10,85,62,2 };
16     try {
17         auto it = find_value(vec.begin(), vec.end(), 65);
18         if (it == vec.end()) throw runtime_error("can not find it!");
19         cout << *it << endl;
20     }
21     catch (runtime_error err) {
22         cout << err.what() << endl;
23     }
24     list<string> li{ "ads", "56", "asdqw5"};
25     auto it1 = find_value(li.begin(), li.end(), "ads");
26     cout << *it1 << endl;
27     system("pause");
28     return 0;
29 }
30 
31 template<typename T, typename U>
32 T find_value(T beg, T end, U value)
33 {
34     auto iter = beg;
35     while (iter != end) {
36         if (*iter == value)
37             return iter;
38         else
39             iter++;
40     }
41     return end;
42 }

练习16.5

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <vector>
 6 #include <list>
 7 
 8 using namespace std;
 9 
10 template<unsigned T, typename U>
11 void print(U(&arr)[T]);
12 
13 int main()
14 {
15     char str[8] = "asdasd";
16     print(str);
17     system("pause");
18     return 0;
19 }
20 
21 template<unsigned T, typename U>
22 void print(U(&arr)[T])
23 {
24     for (auto c : arr)
25         cout << c;
26 }

练习16.6

将数组实参传入模板函数,返回首尾的迭代器;

#include <iostream>
#include <string>
#include <utility>
#include <memory>
#include <vector>
#include <list>

using namespace std;

template<typename T, unsigned U>
T* begin_of_array(T(&arr)[U]);

template<typename T, unsigned U>
T* end_of_array(T(&arr)[U]);

int main()
{
    char str[7] = "asdasd";
    cout << *(begin_of_array(str)) << endl;
    cout << *(end_of_array(str)) << endl;
    system("pause");
    return 0;
}

template<typename T, unsigned U>
T * begin_of_array(T(&arr)[U])
{
    T* beg = arr;
    return beg;
}

template<typename T, unsigned U>
T * end_of_array(T(&arr)[U])
{
    auto end = &arr[U - 2];        //数组从0开始且以\0结束
    return end;
}

练习16.7

 1 #include <iostream>
 2 #include <string>
 3 #include <utility>
 4 #include <memory>
 5 #include <vector>
 6 #include <list>
 7 
 8 using namespace std;
 9 
10 template<typename T, unsigned U>
11 constexpr int size(T(&arr)[U]);
12 
13 int main()
14 {
15     char str[7] = "asdasd";
16     cout << size(str) << endl;
17     system("pause");
18     return 0;
19 }
20 
21 template<typename T, unsigned U>
22 constexpr int size(T(&arr)[U])
23 {
24     return U;
25 }

练习16.8

所有的标准库容器的迭代器都定义了==和!=,但是他们中的大多数都没有定义<运算符。

转载于:https://www.cnblogs.com/wuyinfenghappy/p/7545209.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值