C++提高编程

这篇具有很好参考价值的文章主要介绍了C++提高编程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

        ●本阶段主要针对C++泛型编程和STL技术做详细讲解,探讨C++更深层的使用

1模板


1.1模板的概念
模板就是建立通用的模具,大大提高复用性
例如生活中的模板
一寸照片模板:

C++提高编程

 1.2函数模板
        ●C++另一种编程思想称为泛型编程,主要利用的技术就是模板
        ●C++提供两种模板机制:函数模板和类模板
1.2.1函数模板语法
函数模板作用:
建立一个通用函数,其函数返回值类型和形参类型可以不具体制定,用一个虚拟的类型来代表。
语法:

template <typename T>
函数声明或定义

解释:
template ---声明创建模板
typename --- 表面其后面的符号是- -种数据类型,可以用class代替
T---通用的数据类型,名称可以替换,通常为大写字母
示例:

#include <iostream>
using namespace std;

// 两个整型交换函数
void swapInt(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

// 两个浮点型交换函数
void swapFloat(float &a, float &b)
{
    float temp = a;
    a = b;
    b = temp;
}

// 函数模板
template <typename T> // 声明一个模板,告诉编译器后面代码中紧跟着的T不要报错,T是-一个通用数据类型

void mySwap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

void test01()
{
    int a = 10;
    int b = 11;
    // 两种方式使用函数模板

    // 1、自动类型推导
    // mySwap(a,b);
    // 2、显示指定类型
    mySwap<int>(a, b);

    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
}

int main()
{
    test01();

    system("pause");
    return 0;
}

总结:
●函数模板利用关键字template
●使用函数模板有两种方式:自动类型推导、显示指定类型
●模板的目的是为了提高复用性,将类型参数化

1.2.2函数模板注意事项
注意事项:
●自动类型推导,必须推导出一致的数据类型T,才可以使用
●模板必须要确定出T的数据类型,才可以使用
示例:

#include <iostream>
using namespace std;

// 函数模板注意事项
template <class T> // typename可以替换class

void mySwap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

// 1、自动类型推导,必须推导出一致的数据类型T才可以使用
void test01()
{
    int a = 10;
    int b = 11;
    char c = 'c';

    mySwap(a, b); // 正确!

    // mySwap(a, c); //错误!推导不出一致的I类型

    cout << "a=" << a << endl;
    cout << "b=" << b << endl;
}

// 2、模板必须要确定出T的数据类型,才可以使用
template <class T>
void func()
{
    cout << "func 调用" << endl;
}
void test02()
{
    func<int>();
}

int main()
{
    test01();

    system("pause");
    return 0;
}

总结:
●使用模板时必须确定出通用数据类型T,并且能够推导出一致的类型
1.2.3函数模板案例
案例描述:
●利用函数模板封装一个排序的函数, 可以对不同数据类型数组进行排序
●排序规则从大到小,排序算法为选择排序
●分别利用char数组和int数组进行测试
示例:
 

#include <iostream>
using namespace std;

// 实现通用  对数组进行排序的函数
// 规则  从大到小
// 算法  选择
// 测试  char数组、int数组

// 交换函数模板
template <class T>
void mySwap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

// 排序算法
template <class T>
void mySort(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        int max = i; // 认定最大值
        for (int j = i + 1; j < len; j++)
        {
            // 认定的最大值比比遍历出的数值 要小 说明j下标的元素才是真正的最大值[
            if (arr[max] < arr[j])
            {
                max = j;
            }
        }
        if (max != i)
        {
            // 交换max和i下标元素
            mySwap(arr[max], arr[i]);
        }
    }
}
// 提供打印数组模板
template <class T>
void printArray(T arr[], int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}
void test01()
{
    // 测试char数组
    char charArr[] = "jiojofd";
    int num = sizeof(charArr);
    mySort(charArr, num);
    printArray(charArr, num);
}
void test01()
{
    // 测试int数组
    int intArr[] = {7, 5, 6, 3, 7};
    int num = sizeof(intArr);
    mySort(intArr, num);
    printArray(intArr, num);
}
int main()
{
    system("pause");
    return 0;
}

1.2.4模板的局限性
局限性:
●模板的通用性并不是万能的

例如:

template <class T>
void f(T a, T b)
{
    a = b;
}

在上述代码中提供的赋值操作,如果传入的a和b是一个数组, 就无法实现了
再例如:

template <class T>
void f(T a, T b)
{
    if (a > b)
    {
        ...
    }
}

因此C++为了解决这种问题,提供模板的重载,可以为这些特定的类型提供具体化的模板
示例:

#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};
// 普通函数模板
template <class T>
bool myCompare(T &a, T &b)
{
    if (a == b)
    {
        return true;
    }
    else
    {
        return false;
    }
}
// 具体化, 显示具体化的原型和定意思以template<>开头,并通过名称来指出类型
// 具体化优先于常规模板
template <>
bool myCompare(Person &p1, Person &p2)
{
    if (p1.m_Name == p2.m_Name && p1.m_Age == p2.m Age)
    {
        return true;
    }
    else
        return false;
}
void test01()
{
    int a = 10;
    int b = 20;
    // 内置数据类型可以直接使用通用的函数模板
    bool ret = myCompare(a, b);
    if (ret)
    {
        cout << "a == b" << endl;
    }
    else

    {
        cout << "a!=b" << endl;
    }
}
void test02()
{
    Person p1("Tom", 10);
    Person p2("Tom", 10);
    // 自定义数据类型,不会调用普通的函数模板
    // 可以创建具体化的Person数据类型的模板,用于特殊处理这个类型
    bool ret = myCompare(p1, p2);
    if (ret)
    {
        cout << "p1 == p2" << endl;
    }
    else
    {
        cout << "p1 != p2" << endl;
    }
}
int main()
{
    test01();
    test02();
    system("pause");
    return 0;
}

总结:
●利用具体化的模板,可以解决自定义类型的通用化
●学习模板并不是为了写模板,而是在STL能够运用系统提供的模板
1.3类模板
1.3.1类模板语法
类模板作用:
●建立一个通用类,类空的成员数据类型可以不具体制定,用一个虚拟的类型来代表。
语法:

template<typename T>
类

解释:
template --声明创建模板
typename -- 表面其后面的符号是-种数据类型,可以用class代替
T通用的数据类型, 名称可以替换,通常为大写字母
示例:
 

#include <iostream>
using namespace std;
#include <string>
// 类模板
template <class NameType, class AgeType>
class Person
{
public:
    Person(NameType name, AgeType age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    void showPerson()
    {
        count << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
    }
    string m_Name;
    int m_Age;
};
void test01()
{
    Person<string, int> p1("孙悟空", 999);
    p1.showPerson();
}

总结:类模板和函数模板语法相似,在声明模板template后面加类,此类称为类模板
1.3.2类模板与函数模板区别I
类模板与函数模板区别主要有两点:
1.类模板没有自动类型推导的使用方式
2.类模板在模板参数列表中可以有默认参数
示例:

// 类模板与函数模板的区别
template <class NameType, class AgeType = int>
class Person
{
public:
    Person(NameType name, AgeType age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    void showPerson()
    {
        count << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
    }
    string m_Name;
    int m_Age;
};
// 1.类模板没有自动类型推导的使用方式
void test01()
{
    // Person p("孙悟空", 999);  错误,无法用自动类型推导
    Person<string, int> p1("孙悟空", 999); // 正确,只能用显示指定类型

    p1.showPerson();
}
// 2.类模板在模板参数列表中可以有默认参数
void test02()
{
    // Person p("孙悟空", 999);  错误,无法用自动类型推导
    Person<string> p1("猪八戒", 999); // 正确,只能用显示指定类型

    p1.showPerson();
}

1.3.3类模板对象做函数参数
学习目标:
●类模板实例化出的对象,向函数传参的方式一共有三种传入方式:
1.指定传入的类型--- 直接显示对象的数据类型
2.参数模板化--- 将对象中的参数变为模板进行传递
3.整个类模板化--- 将这个对象类型模板化进行传递

示例:

#include <iostream>
using namespace std;
#include <string>
// 类模板对象做函数参数
template <class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    void showPerson()
    {
        count << "name:" << this->m_Name << ",age:" << this->m_Age << endl;
    }
    T1 m_Name;
    T2 m_Age;
};
// 1.指定传入的类型--- 直接显示对象的数据类型
void printPerson1(Person<string, int> &p)
{
    p.showPerson();
}
void test01()
{
    Person<string, int> p("孙悟空", 100);
    printPerson1(p);
}
// 2.参数模板化--- 将对象中的参数变为模板进行传递
template <class T1, class T2>
void printPerson2(Person<T1, T2> &p)
{
    p.showPerson();
    cout << "T1的类型为: " << typeid(T1).name() << endl;
    cout << "T2 的类型为:" << typeid(T2).name() << endl;
}
void test02()
{
    Person<string, int> p("猪八戒", 80);
    printPerson2(p);
}
// 3.整个类模板化--- 将这个对象类型模板化进行传递
template <class T>
void printPerson3(T &p)
{
    p.showPerson();
    cout << "T 的类型为:" << typeid(T).name() << endl;
}
void test03()
{
    Person<string, int> p("沙和尚", 100);
    printPerson3(p);
}

总结:
●通过类模板创建的对象,可以有三种方式向函数中进行传参
●使用比较广泛是第一-种:指定传入的类型

1.3.4类模板与继承
当类模板碰到继承时,需要注意一下几点:
●当子类继承的父类是一 个类模板时,子类在声明的时候,要指定出父类中T的类型
●如果不指定,编译器无法给子类分配内存
●如果想灵活指定出父类中T的类型,子类也需变为类模板
 

#include <iostream〉
using namespace std;
// 类模板与继承
template <class T>
class Base
{
    T m;
};
// class Son :public Base //错误,必须要知道父类中的T类型,才能继承给子类
class Son : public Base<int>
{
};
void test01()
{
    Son s1;
};
// 如果想灵活指定父类中T类型,子类也需要变类模板
template <class T1, class T2>
class Son2 : public Base<T2>
{
public:
    Son2()
    {
        cout << "T1的类型为: " << typeid(T1).name() << endl;
        cout << "T2的类型为:" << typeid(T2).name() << endl;
    }
    T1 obj;
};
void test02()
{
    Son2<int, char> S2;
}

总结:如果父类是类模板,子类需要指定出父类中T的数据类型
1.3.5类模板成员函数类外实现
学习目标:能够掌握类模板中的成员函数类外实现

#include <iostream>
using namespace std;
#include <string>
// 类模板成员函数类外实现
template <class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age);
    // {
    //     this->m_Age = name;
    //     this->m_Age = age;
    // }
    void showPerson();
    // {
    //     cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
    // }
    T1 m_Name;
    T2 m_Age;
};

// 构造函数类外实现
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2 age)
{
    this->m_Age = name;
    this->m_Age = age;
}
// 成员函数的类外实现
template <class T1, class T2>
void Person<T1, T2>::showPerson()
{
    cout << "姓名:" << this->m_Name << "年龄:" << this->m_Age << endl;
}
void test01()
{
    Person<string, int> P("Tom", 20);
    P.showPerson();
}

1.3.6类模板分文件编写
学习目标: 
●掌握类模板成员函数分文件编写产生的问题以及解决方式
问题:
●类模板中成员函数创建时机是在调用阶段,导致分文件编写时链接不到
解决:
●解决方式1:直接包含.cpp源文件
●解决方式2:将声明和实现写到同一个文件中,并更改后缀名为.hpp, hpp是约定的名称,并不是强制
示例:
person.hpp中代码:

#include <iostream>
using namespace std;
#include <string>
// 类模板份文件编写问题以及解决
template <class T1, class T2>
class Person
{
public:
    Person(T1 name, T2 age);
    void showPerson();
    T1 m_Name;
    T2 m_age;
};
template <class T1, class T2>
Person<T1, T2>::Person(T1 name, T2, age)
{
    this->m_Name = name;
    this->m_Age = age;
}

template <class T1, class T2>
void Person<T1, T2>::showPerson()
{
    cout << "姓名;" << this->m_Name << "年龄" << this->m_age << endl;
}

类模板分文件编写.cpp中代码

#include <iostream>
using namespace std;
// #include "person.h"
#include "person.cpp" //解决方式1,包含cpp源文件
// 解决方式2, 将声明和实现写到一起,文件后缀名改为 .hpp
#include "person.hpp"
void test01()
{
    Person<string, int> p("Tom", 10);
    p.showPerson();
}
int main()
{
    test01();
    system("pause");
}

总结:主流的解决方式是第二种,将类模板成员函数写到-起,并将后缀名改为.hpp

1.3.7类模板与友元
学习目标:
●掌握类模板配合友元函数的类内和类外实现
全局函数类内实现-直接在类内声明友元即可
全局函数类外实现-需要提前让编译器知道全局函数的存在
示例:

#include <iostream>
using namespace std;
#include <string>
// 通过全局函数打印Person信息

// 提前让编译器知道Person类存在
template <class T1, class T2>
class Person;
// 类外实现
template <class T1, class T2>
void printPerson2(Person<T1, T2> p)
{
    cout << "姓名 :" << p.m_Name << "年龄 :" << p.m_Age << endl;
}

template <class T1, class T2>
class Person
{
    // 全局函数类内实现

    friend void printPerson(Person<T1, T2> p)
    {
        cout << "姓名 :" << p.m_Name << "年龄 :" << p.m_Age << endl;
    }

    // 全局函数类外实现
    // 加空模板的参数列表
    // 如果全局函数是类外实现,需要让编译器提前知道这个函数的存在
    friend void printPerson2<>(Person<T1, T2> p);

public:
    Person(T1 name, T2 age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

private:
    T1 m_Name;
    T2 m_Age;
};

// 1、全局函数在类内实现
void test01()
{
    Person<string, int> p("Tom", 20);
    printPerson(p);
}
// 1、全局函数在类外实现
void test02()
{
    Person<string, int> p("jj", 20);
    printPerson2(p);
}

1.3.8类模板案例
案例描述:实现-个通用的数组类,要求如下:
●可以对内置数据类型以及自定义数据类型的数据进行存储
●将数组中的数据存储到堆区
●构造函数中可以传入数组的容量
●提供对应的拷贝构造函数以及operator=防止浅拷贝问题
●提供尾插法和尾删法对数组中的数据进行增加和删除
●可以通过下标的方式访问数组中的元素
●可以获取数组中当前元素个数和数组的容量
解题思路:
C++提高编程

 示例:

myArray.hpp中代码
 

// 自己通用的数组类
#pragma once
#include <iostream>
using namespace std;

template <class T>
class MyArray
{
public:
    // 有参构造,参数  容量
    MyArray(int capacity)
    {
        cout << "MyArray的有参构造" << endl;
        this->m_Capacity = capacity;
        this->m_Size = 0;
        this->pAddress = new T[this->m_Capacity];
    }
    // 拷贝构造
    MyArray(const MyArray &arr)
    {
        cout << "MyArray的拷贝构造" << endl;
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        // this->pAddress = arr.pAddress;

        // 深拷贝
        this->pAddress = new T[arr.m_Capacity];

        // 将arr中的数据都拷贝过来
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
    }

    // oprator=  防止浅拷贝问题
    MyArray &operator=(const MyArray arr)
    {
        cout << "MyArray的oprator=调用" << endl;
        // 先判断来源堆区是否有数据,如果有先释放
        if (this->pAddress != NULL)
        {
            delete[] this->pAddress;
            this->pAddress = NULL;
            this->m_Capacity = 0;
            this->m_Size = 0;
        }

        // 深拷贝
        this->m_Capacity = arr.m_Capacity;
        this->m_Size = arr.m_Size;
        this->pAddress = new T[arr.m_Capacity];
        for (int i = 0; i < this->m_Size; i++)
        {
            this->pAddress[i] = arr.pAddress[i];
        }
        return *this;
    }
	 // 尾插法
    void Push_Back(const T &val)
    {
        // 判断容量是否等于大小
        if (this->m_Capacity == this->m_Size)
        {
            return;
        }
        this->pAddress[this->m_Size] = val;
        this->m_Size++;
    }
    // 尾删法
    void Pro_Back()
    {
        // 让用户访问不到最后一个元素,即为尾删,逻辑删除
        if (this->m_Size == 0)
        {
            return;
        }
        this->m_Size--;
    }
    // 通过下标的方式访问数组中的元素 arr[0]
    T &operator[](int index)
    {
        return this->pAddress[index];
    }

    // 返回数组容量
    int getCapacity()
    {
        return this->m_Capacity;
    }
    // 返回数组大小
    int getSize()
    {
        return this->m_Size;
    }

    // 析构函数
    ~MyArray()
    {
        if (this->pAddress != NULL)
        {
            cout << "MyArray的析构调用" << endl;
            delete[] this->pAddress;
            this->pAddress = NULL;
        }
    }

private:
    T *pAddress; // 指针指向堆区开辟的真是数组

    int m_Capacity; // 数组容量

    int m_Size; // 数组大小
};

类模板案例一数组类封装.cpp中

#include <iostream>
using namespace std;
#include "Myarray.hpp"
#include <string>

void printIntArray(MyArray<int> &arr)
{
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout << arr[i] << endl;
    }
}
void test01()
{

    MyArray<int> arr1(5);
    for (int i = 0; i < 5; i++)
    {
        // 利用尾插法向数组中插入数据
        arr1.Push_Back(i);
    }
    cout << "arr1的打印输出为:" << endl;
    printIntArray(arr1);
    cout << "arr1的容量:" << arr1.getCapacity() << endl;
    cout << "arr1的大小" << arr1.getSize() << endl;

    MyArray<int> arr2(arr1);
    arr2.Pro_Back();
    printIntArray(arr2);
    cout << "arr2尾删后:" << endl;
    cout << "arr2的容量:" << arr2.getCapacity() << endl;
    cout << "arr2的大小" << arr2.getSize() << endl;
    // MyArray<int> arr3(100);
    // arr3 = arr1;
}

// 测试自定义的数据类型
class Person
{
public:
    Person(){};
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    string m_Name;
    int m_Age;
};
void pringPersonArray(MyArray<Person> &arr)
{
    for (int i = 0; i < arr.getSize(); i++)
    {
        cout << "姓名:" << arr[i].m_Name << "年龄:" << arr[i].m_Age << endl;
    }
};
void test02()
{
    MyArray<Person> arr(10);
    Person p1("孙悟空", 100);
    Person p2("赵云", 30);
    Person p3("妲己", 20);
    Person p4("王孙", 40);
    Person p5("李四", 100);

    // 将数据插入到数组中
    arr.Push_Back(p1);
    arr.Push_Back(p2);
    arr.Push_Back(p3);
    arr.Push_Back(p4);
    arr.Push_Back(p5);

    // 打印数组
    pringPersonArray(arr);

    // 输出容量和大小
    cout << "arr的容量:" << arr.getCapacity() << endl;
    cout << "arr的大小" << arr.getSize() << endl;
	//arr3=arr1;
};


int main()
{
    //test01();
	test02();
	system("pause"); 
	return 0;
};

总结:
能够利用所学知识点实现通用的数组
 文章来源地址https://www.toymoban.com/news/detail-436267.html

到了这里,关于C++提高编程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】——模板(泛型编程+函数模板+类模板)

    之前我们学习了函数重载,让我们在写相似函数的时候非常方便,但函数重载还有很多不足的地方,比如,每次写相似函数的时候,都要我们重新重载一个逻辑、代码几乎一样的函数,这就导致了我们的效率变低,所以我们今天来学习C++模板的相关知识点,学习完模板之后,

    2024年02月05日
    浏览(36)
  • C++——模板初阶与泛型编程

    🌸作者简介: 花想云 ,在读本科生一枚,致力于 C/C++、Linux 学习。 🌸 本文收录于 C++系列 ,本专栏主要内容为 C++ 初阶、C++ 进阶、STL 详解等,专为大学生打造全套 C++ 学习教程,持续更新! 🌸 相关专栏推荐: C语言初阶系列 、 C语言进阶系列 、 数据结构与算法 本章我们

    2023年04月17日
    浏览(30)
  • 第十二站(20天):C++泛型编程

    C++提供了模板(template)编程的概念。所谓模板,实际上是建立一个通用函数或类, 其 类内部的类型和函数的形参类型不具体指定 ,用一个虚拟的类型来代表。这种通用的方式称 为模板。 模板是泛型编程的基础, 泛型编程即以一种独立于任何特定类型的方式编写代码 如: vect

    2024年01月22日
    浏览(26)
  • 【C++】从零开始认识泛型编程 — 模版

    送给大家一句话: 尽管眼下十分艰难,可日后这段经历说不定就会开花结果。总有一天我们都会成为别人的回忆,所以尽力让它美好吧。 – 岩井俊二 \\ ⱶ˝୧(๑ ⁼̴̀ᐜ⁼̴́๑)૭兯 //// \\ ⱶ˝୧(๑ ⁼̴̀ᐜ⁼̴́๑)૭兯 //// \\ ⱶ˝୧(๑ ⁼̴̀ᐜ⁼̴́๑)૭兯

    2024年04月28日
    浏览(29)
  • 【C++】泛型编程 ② ( 函数模板与普通函数区别 )

    函数模板与普通函数区别 : 主要区别在于它们能够处理的 数据类型数量 和 灵活性 ; 自动类型转换 : 函数模板 不允许 自动类型转化 , 会进行严格的类型匹配 ; 普通函数 能够进行 自动类型转换 , 内含隐式的类型转化 ; 参数 / 返回值 数据类型 : 普通函数 只接受 特定类型 参数

    2024年02月20日
    浏览(28)
  • 【C++初阶】八、初识模板(泛型编程、函数模板、类模板)

    ========================================================================= 相关代码gitee自取 : C语言学习日记: 加油努力 (gitee.com)  ========================================================================= 接上期 : 【C++初阶】七、内存管理 (C/C++内存分布、C++内存管理方式、operator new / delete 函数、定位new表

    2024年02月04日
    浏览(32)
  • 【c++ primer 笔记】第 16章 模板与泛型编程

    🎉作者简介:👓 博主在读机器人研究生,目前研一。对计算机后端感兴趣,喜欢 c + + , g o , p y t h o n , 目前熟悉 c + + , g o 语言,数据库,网络编程,了解分布式等相关内容 textcolor{orange}{博主在读机器人研究生,目前研一。对计算机后端感兴趣,喜欢c++,go,python,目前熟悉c+

    2024年02月20日
    浏览(28)
  • 【C++】泛型编程 ③ ( 函数模板 与 普通函数 调用规则 | 类型匹配 | 显式指定函数模板泛型类型 )

    上一篇博客 【C++】泛型编程 ② ( 函数模板与普通函数区别 ) 中 , 分析了 函数参数 类型匹配 下的 普通函数 与 函数模板 的调用规则 ; 为 函数模板 重载了 普通函数 , 普通函数有指定的类型 ; 传入实参 , 调用 普通函数 还是 模板函数 , 是有一定的规则的 ; 普通函数 与 传入实

    2024年02月21日
    浏览(30)
  • 【C++】C++编程提高代码的性能

    👉博__主👈:米码收割机 👉技__能👈:C++/Python语言 👉公众号👈:测试开发自动化【获取源码+商业合作】 👉荣__誉👈:阿里云博客专家博主、51CTO技术博主 👉专__注👈:专注主流机器人、人工智能等相关领域的开发、测试技术。 高性能处理是C++中一个重要的应用领域,

    2024年02月06日
    浏览(43)
  • C++提高编程

            ●本阶段主要针对C++泛型编程和STL技术做详细讲解,探讨C++更深层的使用 1模板 1.1模板的概念 模板就是建立通用的模具,大大提高复用性 例如生活中的模板 一寸照片模板:  1.2函数模板         ●C++另一种编程思想称为泛型编程,主要利用的技术就是模板  

    2024年02月03日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包