美文网首页
C++入门11 -- 仿函数,函数模版,类模版

C++入门11 -- 仿函数,函数模版,类模版

作者: YanZi_33 | 来源:发表于2021-08-16 12:10 被阅读0次

仿函数(函数对象)

  • 仿函数:将一个对象当作一个函数来使用;
//  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类的模版中的函数声明与实现是分离的,注意语法格式;

相关文章

  • C++入门11 -- 仿函数,函数模版,类模版

    仿函数(函数对象) 仿函数:将一个对象当作一个函数来使用; 模版 泛型:将类型参数化以达到代码复用的技术,C++中...

  • 模版

    模版 模版函数 使用模版函数不需要指定类型,直接传参就可以了。 模版类 使用模版类需要指定类型。

  • GeekBand STL与泛型编程 First Week

    GeekBand STL与泛型编程 First Week 泛型编程 模版介绍 模版是C++的一种特性,允许函数或类...

  • function可调用对象模版类

    一、std::function介绍 std::function可调用函数对象模版类是一个函数包装器模版,该函数...

  • 日记之旅第三天

    上午:1.学习C++中函数4中特性,函数返回值,函数形参,函数模版,其中模版最为难以理解,值得自己后面好好研究下下...

  • 网易云C++第五周笔记(GeekBand)

    1.C++模板C++模版是C++泛型编程的基础,一个模版就是一个创建类或函数的公式,比如我们在比较大小时,一般要根...

  • 【极客班】《 STL与泛型编程第一周》学习笔记

    1. 模版简介 1) c++模板简介 c++中,模板可以允许函数或者类通过泛型的方式来表现或者运行。例如,我们可以...

  • 自用VA注释模版

    函数注释 模版内容: //============================================...

  • C++ 模版 学习总结

    C++ 模版 模板是C++支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据...

  • 2017-03-15 C++阶段

    Task List 1. 回顾引用与new用法 2. 学习模版template、重载函数、类(构造函数) Summ...

网友评论

      本文标题:C++入门11 -- 仿函数,函数模版,类模版

      本文链接:https://www.haomeiwen.com/subject/huymbltx.html