练习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
所有的标准库容器的迭代器都定义了==和!=,但是他们中的大多数都没有定义<运算符。