c++模板 理解(图、文、代码)

本文介绍了C++中的模板基础知识,包括函数模板用于实现对不同类型数据的通用打印函数,以及类模板用于创建泛型类。文章通过示例详细解释了常引用(const&)的作用,并展示了如何在模板中使用。此外,还讨论了多参数模板、参数包和类模板的外部定义及友元函数的应用。
摘要由CSDN通过智能技术生成

嗯,又是发个重点,拿出来单独做笔记

本文有参考以下博文:

1、C++ template \ auto_c++ template auto_rainbow_lucky0106的博客-CSDN博客

2、C++ 中的 const & (常引用)参数 - 知乎

3、C++ template \ auto_c++ template auto_rainbow_lucky0106的博客-CSDN博客

4、C++模板类中的成员函数以及模板函数在类外定义的方式_模板类构造函数类外定义_云飞扬_Dylan的博客-CSDN博客

博主编译器版本是c++17

正常情况你打印一个数组

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
// 程序的主函数

int main()
{
    SetConsoleOutputCP(65001); 
    vector<int> v={7,5,10,12};
    v.push_back(26);//类似js的push你懂的
    cout<<"[";
    for(int n:v){
        cout<<n<<" ";
    }
    cout<<"]"<<endl;
    v.pop_back();
    cout<<"[";
    for(int n:v){
        cout<<n<<" ";
    }
    cout<<"]";
}

你们觉不觉得这个循环打印每次都要写一遍很烦?

那咱就给他弄成个函数呗

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
void print(vector<int> arr){
    cout<<"[";
    for(int n:arr){
        cout<<n<<" ";
    }
    cout<<"]"<<endl;
}
int main()
{
    SetConsoleOutputCP(65001); 
    vector<int> v={7,5,10,12};
    v.push_back(26);//类似js的push你懂的
    print(v);
    v.pop_back();
    print(v);
}

 好,这样我们就得到了一个int型数组的打印,那如果我说我现在要打印一个char型的你是不是得改变量,

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
void print(vector<int> arr){
    cout<<"[";
    for(int n:arr){
        cout<<n<<" ";
    }
    cout<<"]"<<endl;
}
void printStr(vector<string> arr){
    cout<<"[";
    for(string n:arr){
        cout<<n<<" ";
    }
    cout<<"]"<<endl;
}
int main()
{
    SetConsoleOutputCP(65001); 
    vector<string> v={"你好啊","b","c","d"};
    v.push_back("e");//类似js的push你懂的
    printStr(v);
    v.pop_back();
    printStr(v);
}

那有没有办法写一个函数,兼容string和int,都可以打印的办法,!有,模板

函数模板

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
template <typename T>
void print(vector<T> arr){
    cout<<"[";
    for(T n:arr){
        cout<<n<<" ";
    }
    cout<<"]"<<endl;
}
int main()
{
    SetConsoleOutputCP(65001);
    //字符串
    vector<string> v={"你好啊","b","c","d"};
    v.push_back("e");//类似js的push你懂的
    print(v);
    v.pop_back();
    print(v);
    //int型
    vector<int> i={1,2,3,4};
    i.push_back(5);//类似js的push你懂的
    print(i);
    i.pop_back();
    print(i);
}

 那C++ 模板 | 菜鸟教程 这里的inline T 是啥意思

来,你先去看博主的这一篇,看完在回来接下去往下看  ====》const&和int& const 理解笔记(图、代码、讲解)_雪狼之夜的博客-CSDN博客

你如果看完博主↑↑↑↑↑↑↑↑这篇完,你会知道const&干啥用的和为什么用它!!!然后你再看博主下面这个代码就不费劲了,

#include <iostream>
#include <windows.h>
using namespace std;

int const& CYC(int const& x,int const& y){
   return x>y?x:y;
}
int main()
{
    SetConsoleOutputCP(65001);
    int a=1;
    int b=2;
    int c=CYC(a,b);
    cout << c;
}

 来,这算进正题了

上面例子咱这么理解,

1、(int const& x,int const& y)代表形参是常整型引用(严谨点应该叫常量 int型 引用,叫习惯就好),

2、int const& CYC()的int const& 代表返回也是常整形引用。那这个例子就是代表,传入和返回的值是不能改的 都必须是长整型引用!!!!!!!!!!!!!!!!!!!!!!!!!

扩展下,你甚至可以再cyc里写 如下  返回

const int b=333;
const int* p;
p=&b;
return *p;

也就是你可以拿形参判断完后  返回别的常整形引用。

那我们带入模板。你对比下上面的代码和下面的代码

#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
T const& CYC(T const& x,T const& y){ //int const&
    // x=3;  因为是常量,所有你x不能给他赋值
   return x>y?x:y;
}
int main()
{
    SetConsoleOutputCP(65001);
    int a=1;
    int b=2;
    int c=CYC(a,b);
    cout << c;
}

嗯,应该很好理解  把int的地方都替换成T。就跟我们博文最开始的那个地方一样,就是替换变量而已。所以你再去看  教程网的例子,就应该很好理解了。

来扩展下实验

#include <iostream>
#include <windows.h>
using namespace std;
template <typename T>
void CYC(T const& x,T const& y){ //int const&
   
}
int main()
{
    SetConsoleOutputCP(65001);
    int a=1;
    string b="你好啊";
    CYC(a,b);
}

上面报错了哦!!!!!!!!!!!!!!!!

 第一个是int 第二个是string,就报错了!,因为T不可能即是int又是string,那咋整,看下面代码

嗯,是这样的,咱定义两个自己T就可以拉。

#include <iostream>
#include <windows.h>
using namespace std;
template <typename T,typename T2>
void CYC(T const& x,T2 const& y){ //int const&
   
}
int main()
{
    SetConsoleOutputCP(65001);
    int a=1;
    string b="你好啊";
    CYC(a,b);
}

问题又来了,那返回值怎么整??看下面代码

#include <iostream>
#include <windows.h>
using namespace std;
template <typename T,typename T2>//
auto const& CYC(T const& x,T2 const& y){ //int const&
   return x;
//    return y;
}
int main()
{
    SetConsoleOutputCP(65001);
    int a=1;
    string b="你好啊";
    cout<< CYC(a,b);
}

 你是不是懂了,

至于auto可以不可以替换成T3,博主试了下,发现是不行的,如果知道怎么 操作的小伙伴麻烦评论区告诉下博主,谢谢

 形参参数包  Args

也就是你可以传多个参数进去函数,(这个里面只能是同类型比如都是int 或者都是string)因为你没办法args[1]  、args[2]这样调用他

#include <iostream>
#include <windows.h>
using namespace std;
void FormatPrint()
{
    std::cout << std::endl;
}
template <class ...Args> // 多个参数
void CYC(Args... args){ //int const&
    int arr[] = { args... }; //列表初始化
    //打印参数包中的各个参数
    for (auto e : arr) {
      cout << e << " ";
    }
    cout << endl;
  //以下可以打印
  // using var = int[];
  // (void)var{0, (cout << args << endl, 0)...};
   
}
int main()
{
    SetConsoleOutputCP(65001);
    // int a=4;
    // int b=2;
    // string b="你好啊";  //不能同时传  char和int
    CYC(10,15,7);
}

c++ 类模板

先从简单的开始,如下,这个是一个普通类

#include <iostream>
#include <windows.h>
using namespace std;
class CYC{
    public:
      int a=1;
      CYC(int x){//构造函数
          this->a=x;
      };
      void get(){
          cout <<this->a;
      };
};

int main()
{
    SetConsoleOutputCP(65001);
    CYC cyc(5);
    cyc.get();
}

 来,把类型int替换掉,改成模板

#include <iostream>
#include <windows.h>
using namespace std;
template<typename T>
class CYC{
    public:
      T a=1;
      CYC(T x){//构造函数
          this->a=x;
      };
      void get(){
          cout <<this->a;
      };
};

int main()
{
    SetConsoleOutputCP(65001);
    CYC<int> cyc(5);//和函数模板区别这里有多一个<int>传入参数类型申明注意
    cyc.get();
}

嗯,这就有点样子了,上面那个例子你理下,然后我们在把类构造函数类成员函数放类外面定义如下

#include <iostream>
#include <windows.h>
using namespace std;
template<typename T>
class CYC{
    T a=1;
    public:
        CYC(T x);//构造函数
        void get(void);
};
template<typename T>
  CYC<T>::CYC(T x){
      this->a=x;
  }
template<typename T>
    void CYC<T>::get(void){
        cout<< this->a;
    }
int main()
{
    SetConsoleOutputCP(65001);
    CYC<int> cyc(5);//和函数模板区别这里有多一个<int>传入参数类型申明注意
    cyc.get();
}

类模板 友元处理  

不知道申明是友元的 跑博主另一篇博文看===》c++类友元函数理解(图、文、代码)_雪狼之夜的博客-CSDN博客

#include <iostream>
#include <windows.h>
using namespace std;

template<typename T>   //友元要先申明  !!!!!!!!!!!
    void yyuan(T& cyc);

template<typename T>
    class CYC{
        T a=1;
        public:
            CYC(T x);//构造函数
            void get(void);//普通类成员函数
            friend void yyuan<>(CYC<T>& cyc);//友元
    };
template<typename T>
    CYC<T>::CYC(T x){
        this->a=x;
    }
template<typename T>
    void CYC<T>::get(void){
        cout<< this->a<<endl;
    }
template<typename T>    
    void yyuan(T& cyc){
        cout <<cyc.a<<endl;
    }

int main()
{
    SetConsoleOutputCP(65001);
    CYC<int> cyc(5);//和函数模板区别这里有多一个<int>传入参数类型申明注意
    cyc.get();
    yyuan(cyc);
}

 有人问为啥你是template<typename T> 不是 template<class T>,博主肯定的告诉你,这里两个都也可以,功能是一模一样的,习惯问题,来,博主附上文章,大家自己去看====》C++ 中 typename 和 class 的区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪狼之夜

打个赏,让博主知道博文没白写

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值