仿函数(函数对象)
- 仿函数:将一个对象当作一个函数来使用;
// main.cpp
// C++36_仿函数
//
// Created by ljj on 8/16/21.
//
#include <iostream>
using namespace::std;
//int sum(int a,int b){
// return a + b;
//}
class Sum {
public:
int operator()(int a,int b){
return a + b;
}
};
int main(int argc, const char * argv[]) {
Sum sum;
//仿函数 与 函数调用 很相似
cout << sum(10,20) << endl;
cout << sum.operator()(10, 20) << endl;
return 0;
}
模版
- 泛型:将类型参数化以达到代码复用的技术,C++中使用模版来实现泛型;
- 模版的使用格式:
template <typename 或者 class T>
// main.cpp
// C++37_模版
//
// Created by ljj on 8/16/21.
//
#include <iostream>
using namespace::std;
//void swapValues(int &v1,int &v2){
// int tmp = v1;
// v1 = v2;
// v2 = tmp;
//}
//
//void swapValues(double &v1,double &v2){
// double tmp = v1;
// v1 = v2;
// v2 = tmp;
//}
//将数据类型 定义成T类型
//定义的时候 参数泛型
template <class T> void swapValues(T &v1,T &v2){
T tmp = v1;
v1 = v2;
v2 = tmp;
}
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
//调用的时候 指定泛型的具体类型
swapValues<int>(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
double c = 10.0;
double d = 22.0;
swapValues<double>(c, d);
cout << "c = " << c << endl;
cout << "d = " << d << endl;
return 0;
}
模版的本质,在底层会根据参数的不同生成不同的函数;
模版在没有使用时,是不会被实例化出来的;
模版的声明与实现分离时,会导致链接错误;
一般将模版的声明与实现统一放到一个
.hpp
文件中;
// Swap.hpp
// C++37_模版
//
// Created by ljj on 8/16/21.
//
#ifndef Swap_h
#define Swap_h
//将数据类型 定义成T类型
//定义的时候 参数泛型
template <class T> void swapValues(T &v1,T &v2){
T tmp = v1;
v1 = v2;
v2 = tmp;
}
#endif /* Swap_h */
-
.hpp
文件中既有声明也有实现;
多参数模版
template <class T1,class T2> void display(const T1 &v1,const T2 &v2){
cout << v1 << endl;
cout << v2 << endl;
}
类模版
- 自定义实现一个C++的Array数组类模版,代码全部封装在
Array.hpp
文件中,代码如下:
// Array.hpp
// C++37_模版
//
// Created by ljj on 8/16/21.
//
#ifndef Array_h
#define Array_h
#include <iostream>
using namespace::std;
template <class Item> class Array {
friend ostream &operator<<(ostream &,const Array &);
Item *m_data = NULL;
//数组元素个数
int m_size = 0;
//数组容量
int m_capacity = 0;
public:
Array(int capacity);
~Array();
void add(Item value);
Item get(int index);
int size();
Item operator[](int index);
void display();
};
template <class Item>
Array<Item>::Array(int capacity){
if (capacity <= 0) return;
this->m_data = new Item[capacity]{};
this->m_capacity = capacity;
}
template <class Item>
Array<Item>::~Array(){
if (!this->m_data) return;
delete [] this->m_data;
this->m_data = NULL;
}
template <class Item>
void Array<Item>::add(Item value){
if (this->m_size == this->m_capacity) {
cout << "数组已满 需要扩容" << endl;
return;
}
this->m_data[this->m_size++] = value;
}
template <class Item>
Item Array<Item>::get(int index){
if (index < 0 || index >= this->m_size) return 0;
return this->m_data[index];
}
template <class Item>
int Array<Item>::size(){
return this->m_size;
}
template <class Item>
Item Array<Item>::operator[](int index){
return get(index);
}
template <class Item>
void Array<Item>::display(){
cout << "[";
for (size_t i = 0; i < this->m_size; i++) {
cout << this->m_data[i];
if (i != this->m_size - 1) {
cout << ", ";
}
}
cout << "]";
cout << endl;
}
#endif /* Array_h */
- 外界调用测试:
#include "Array.hpp"
int main(int argc, const char * argv[]) {
Array<int> arr1(5);
arr1.add(1);
arr1.add(2);
arr1.add(3);
arr1.add(4);
arr1.display();
Array<double> arr2(5);
arr2.add(10.0);
arr2.add(11.0);
arr2.add(12.0);
arr2.add(13.0);
arr2.display();
Array<Person *> arr3(5);
arr3.add(new Person(10));
arr3.add(new Person(20));
arr3.add(new Person(30));
arr3.add(new Person(40));
arr3.display();
Array<Person> arr4(5);
arr4.add(Person(10));
arr4.add(Person(20));
arr4.add(Person(30));
arr4.add(Person(40));
arr4.display();
return 0;
}
-
template <class Item> class Array
定义一个Array类的模版; - Array类的模版中的函数声明与实现是分离的,注意语法格式;