C++ 学习指南基础(五)

目录

 

121. Class Template Instantiation

122. Default Type and Nontype Parameters

123. Templates and inheritance

124. When/Where to Use Template

125. Standard Template Library

126. STL Containers

127. STL Iterat​or

128. More on Containers


121. Class Template Instantiation

类模版实例化

  1. 类模板实例化

1.1. 显式实例化

template class Stack<int>; // 将类模板实例化为一个处理int类型的Stack类

1.2. 隐式实例化

Stack<char> charStack; // 先实例化一个CharStack类(名字由编译器按规则生成)

                       // class CharStack { … char elements[100]; … };
 ​
                       // 然后用 CharStack charStack; 创建一个对象

Stack<int> intStack; // 实例化一个IntStack类

                       // class IntStack { … int elements[100]; … };
 ​
                       // 然后用 IntStack intStack; 创建一个对象

vector intVector{1, 2, 3}; // C++17,模板类型参数根据初始化语句自动推导

                           // 实例化为 vector<int>

122. Default Type and Nontype Parameters

默认类型与非类型参数

  1. Default type (默认类型)

1.1. Review: default parameter value (复习:参数的默认值)

int circle (int x, int y, int r=100);

1.2. Default type parameter (默认类型参数)

You can assign a default type for a type parameter in a class template. (可以为类模板的类型参数指定一个默认类型)

1.3. 指定泛型Stack的默认类型参数为 int

template<typename T = int>

class Stack{

...

};

1.4. 用默认类型定义一个对象

//a stack for int values

Stack<> stack;

U 警告 U

You can only use default type in class templates, NOT in function templates

  1. Non-type Parameters (非类型参数)

Using nontype parameters in a template prefix. (在模板前缀中使用非类型参数)

When instantiating a template, the nontype argument should be an object(实例化模板时,非类型实参应该是对象)

template<typename T, int capacity>

class Stack{

...

private:

T elements[capacity];

int size;

};

Stack<char, 100> charStack;

//对象作为非类型参数

template<typename T, Color c>

class Label{

……

};

Color color(0, 0, 255);

Label<char, color> label;

123. Templates and inheritance

模板与继承

A non-template class can be derived from a class template specialization(普通类可从类模板实例继承).

A class template can be derived from a nontemplate class.(模板可从普通类继承)

A class template can be derived from a class template.(类模板可继承类模板)

template<typename T> class T1;

template<typename T> class T2;

class C;

124. When/Where to Use Template

何时何地使用模板

  1. When/Where to Use Template? (何时使用模板编程)

1.1. 使用别人写好的模板库时

STL (Standard Template Library)

Boost

1.2. 对不同类型的数据做类似处理 (算法、容器、遍历等)

1.3. 过量使用模板不好(不要模板原教旨主义)

When you want to write code that nobody else on your team can understand

When you want code that you won't be able to understand 7 days after you wrote it

When code performance is more important to you than maintainability

When you want to be able to list "Template Metaprogramming" as a skill on your resumé.

When you need to write code that's unlikely to work on many real-world compilers

  1. OOP or GP?

2.1. C++: Generic programming is very widely useful, and can often to a large extent replace OOP (泛型编程在C++中广泛使用,经常可以取代面向对象编程)

2.2. Almost the entire standard library relies on generic programming (几乎整个C++标准库都依赖于泛型编程)

2.3. There is little inheritance or polymorphism in the standard library (not/incl: exception, string and stream class) (在C++标准库较少使用继承和运行时多态。异常、字符串和IO流中使用了较多的继承)

125. Standard Template Library

第11单元:工欲善其事必先利其器 – 标准模板库

126. STL Containers

第02节: STL容器

Container Classes (容器类)

STL Container Header Applications
vector <vector> 直接访问任意元素,快速插入、删除尾部元素
deque <deque> 直接访问任意元素,快速插入、删除头部和尾部元素
list <list> 快速插入、删除任意位置元素
set <set> 快速查询元素,无重复关键字
multiset <set> 与set相同,但允许重复关键字
map <map> Key/value pair mapping(键值对映射)。不允许重复关键字, 使用关键字快速查询元素
multimap <map> 与map相同,但允许重复关键字
stack <stack> 后进先出容器.
queue <queue> 先进先出容器
priority_queue <queue> 高优先级元素先删除

Common Functions to All Containers (所有容器共同函数)

Functions Description
non-arg constructor 无参构造函数 构造一个空容器
constructor with args 带参构造函数 每个容器都有多个带有参数的构造函数
copy constructor 拷贝构造函数 创建一个容器,从一个已有的同类型容器中复制元素
destructor 析构函数 容器销毁后执行清理工作
empty() 若容器中没有元素则返回空
size() 返回容器中的元素数目
operator= 将容器内容复制到另一个容器
Relational operators (<, <=, >, >=, ==, and !=) 顺序比较两个容器中的对应元素,来确定大小关系

Common Functions to First-Class Containers (一级容器的通用函数)

Functions Description
c1.swap(c2) 交换两个容器c1和c2的内容
c1.max_size() 返回一个容器可以容纳的最大元素数量
c.clear() 删除容器中的所有元素
c.begin() 返回容器首元素的迭代器
c.end() 返回容器尾元素之后位置的迭代器
c.rbegin() 返回容器为元素的迭代器,用于逆序遍历
c.rend() 返回容器首元素之前位置的迭代器,用于逆序遍历
c.erase(beg, end) 删除容器中从beg到end-1之间的元素。 beg和end都是迭代器

Simple Demo

vector<int> vector1, vector2; list<int> list1, list2; deque<int> deque1, deque2; set<int> set1, set2; multiset<int> multiset1, multiset2; stack<int> stack1, stack2; queue<int> queue1, queue2;

127. STL Iterator

STL迭代器

Iterators(迭代器) Iterators are used extensively in the first-class containers for accessing and manipulating the elements. (迭代器用于访问和处理一级容器中的元素) Several functions (e.g., begin() and end()) in the first-class containers are related to iterators. (一级容器中的某些函数也与迭代器有关)

Example 如何使用迭代器访问容器中的元素? 不同容器对于元素的输入输出次序有何影响? vector输出数据的次序与输入次序一致; Sequence Cont. set 输出数据的次序不同于输入次序; Associative Cont.

Type of Iterators(迭代器类型) Each container has its own iterator type. (各容器均有自己的迭代器类型) Iterators can be classified into five categories: (迭代器可分为5类)

uploading.4e448015.gif转存失败重新上传取消

Iterator Types Supported by Containers (容器支持的迭代器类型)

STL Container Type of Iterators Supported

uploading.4e448015.gif转存失败重新上传取消

Operators Supported by Iterators(迭代器支持的运算符)

类别 运算符 描述
All iterators ++p Preincrement an iterator. 前置自增
p++ Postincrement an iterator. 后置自增  
Input iterators *p Dereference an iterator (used as rvalue).迭代器解引用(仅做右值)
p1 == p2 Evaluates true if p1 and p2 point to the same element.  
p1 != p2 Evaluates true if p1 and p2 point to different elements.  
Output iterators *p Dereference an iterator (used as lvalue).迭代器解引用(仅做左值)
Bidirectionl iterators --p Predecrement an iterator. 前置自减
p-- Postdecrement an iterator. 后置自减  
Random access iterators p += i Increment iterator p by i positions.
p -= i Decrement iterator p by i positions.  
p + i Returns an iterator ith position after p.  
p - i Returns an iterator ith position before p.  
p1 < p2 Returns true if p1 is before p2.  
p1 <= p2 Returns true if p1 is before or equal to p2.  
p1 > p2 Returns true if p1 is after p2.  
p1 >= p2 Returns true if p1 is after p2 or equal to p2.  
p[i] Returns the element at the position p offset by i.  

Iterator Demo 注意:例子中的都表现出了哪些类型的的特征?

128. More on Containers

发布了7 篇原创文章 · 获赞 0 · 访问量 286

猜你喜欢

转载自blog.csdn.net/LYR1994/article/details/105485560