【C++ Primer】【学习笔记】【第十六章】模板与泛型编程之一:模板定义
2015-01-06 20:44
330 查看
一、函数模板
格式:
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:模板函数求绝对值
习题16.9:find算法模板
习题16.13:使用模板打印容器元素
习题16.14:使用模板打印容器元素(使用迭代器进行处理)
习题16.15:编写可以确定数组长度的函数模板
习题16.16:编写可以打印不同长度数组的内容的函数模板
格式:
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) ) { 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) ) { 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
相关文章推荐
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义
- C++ Primer复习和学习笔记 第十六章 模板与泛型编程
- C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]
- C++ Primer 学习笔记_76_模板与泛型编程 -模板定义[续]
- C++ Primer 学习笔记_76_模板和泛型编程 --模板定义[继续]
- C++ Primer 学习笔记_79_模板与泛型编程 -模板编译模型
- C++ Primer 学习笔记_82_模板与泛型编程 -类模板成员[续二]
- C++ Primer 学习笔记_77_模板与泛型编程 --实例化
- C++ Primer 学习笔记_80_模板与泛型编程 -类模板成员
- C++ Primer 学习笔记_84_模板与泛型编程 --模板特化
- C++ Primer 学习笔记_83_模板与泛型编程 --一个泛型句柄类
- C++ Primer 学习笔记十六 —— 模板与泛型编程
- C++ Primer 学习笔记_86_模板与泛型编程 -满载与函数模板
- C++ Primer 学习笔记_85_模板与泛型编程 --模板特化[续]
- C++ Primer 学习笔记_77_模板与泛型编程 -实例化
- C++ Primer 学习笔记_84_模板与泛型编程 -模板特化
- C++ primer 模版学习笔记-在模板定义内部指定类型
- C++ Primer 学习笔记_81_模板与泛型编程 -类模板成员[续一]
- C++ Primer 学习笔记_79_模板与泛型编程 --模板编译模型
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义