C++ 宏、函数模板、类模板、

宏定义

#include <iostream>
//#include <string.h>
//using namespace std;

//预处理指令:是在编译过程种,将这些预处理的指令执行,编译后的程序运行时是运行处理后的代码
#define PRICE 3   // 宏定义是替换,类似于文本替换
#undef PRICE      // 取消宏定义
#define PRICE 4   // 重新进行宏定义

int main()
{   
    int n = 1;

    std::cout << n + PRICE << std::endl;
    return 0;
}

泛式编程
宏:只是单纯替换
宏 例子1:

#include <iostream>

#define MAX(a,b) (a > b ? a : b)  // 宏定义

using namespace std;


int main()
{
    cout << MAX(1, 2) << endl;
    cout << MAX(2.4, 2.7) << endl;
    cout << MAX('a', 'c') << endl;
    return 0;
}

宏 例子2:

#include <iostream>
#include <string>
#include <vector>

// #define SQUARE(x) x * x  // 宏定义 错误演示
#define SQUARE(x) ((x) * (x))  // 宏定义 正确演示

using namespace std;


int main()
{
    int res = SQUARE(5);     // 简单替换 5 * 5
    cout << res << endl;

    res = 100 / SQUARE(5);   // 简单替换 100 / 5 * 5  正确演示时:100 / ((5) * (5))

    cout << res << endl;

    return 0;
}

函数模板(重点)

#include <iostream>
#include <string>
#include <vector>


using namespace std;

// int max_(int a, int b)            // int 类型
// {
//     return a > b ? a : b;
// };

// double max_(double a, double b)  // double 类型
// {
//     return a > b ? a : b;
// };

// char max_(char a, char b)        // char 类型
// {
//     return a > b ? a : b;
// };

// 将以上代码 使用函数模板 进行简化

// 定义模板 T 是自定义的
template <typename T>
T max_(T a, T b){
    return a > b ? a : b;
}


// template <class T>  // 也可使用 class
// T max_(T a, T b){
//     return a > b ? a : b;
// }

template <class T1, class T2>   // 多个参数
void display(T1 a, T2 b)
{
    cout << a << " " << b << endl;
}


int main()
{
    // cout << max_(1, 2) << endl;      // 2
    // cout << max_(1.1, 2.2) << endl;  // 2.2
    // cout << max_('a', 'c') << endl;  // c

    cout << max_<int>(1, 2) << endl;   // 为了阅读方便 推荐
    cout << max_(1, 2) << endl;        // 可以不写数据类型
    cout << max_('B', 'A') << endl;
    cout << max_(3.3, 2.2) << endl;
    cout << max_(5 + 2 * 9, 7 + 2 * 4) << endl;

    display<int, int>(1, 2);
    display(20, 30);
    display<char, double>('A', 3.3);
    display("Hello", "World");
    display(2000, string{"Hello"});

    return 0;
}

类模板(重点)

#include <iostream>
#include <string>
#include <vector>


using namespace std;

// 第一个类模板
template<typename T>  // 类模板 自定判断数据类型  T可以换成任意单词
class Item
{
private:
    string name;
    T value;
public:
    Item(string name, T value)                // 注意 T的使用
        :name(name), value(value){}

    string get_name() const { return name; }
    T get_value() const { return value; }    // 注意 T的使用

};

// 第二个类模板
template<typename T1, typename T2>  
struct my_pair  // 类权限默认公有
{
    T1 first;
    T2 second;
};


int main()
{
    // Item<int> a1{"a1", 100};  // int 是参数2 的自定义的数据类型 可以换成任意类型
    // cout << a1.get_name() << " " << a1.get_value() << endl;
    
    // Item<string> a2{"a2", "C++"};  
    // cout << a2.get_name() << " " << a2.get_value() << endl;

    // // 嵌套
    // Item<Item<string>> a3{"a3", {"b", "c"}};  
    // cout << a3.get_name() << " " << a3.get_value().get_name() << " " <<  a3.get_value().get_value() << endl;
    
    // // 放入容器
    // vector<Item<int>> vec;
    // vec.push_back(Item<int>{"v1", 1});
    // vec.push_back(Item<int>{"v2", 2});
    // vec.push_back(Item<int>{"v3", 3});

    // for (auto i:vec){
    //     cout << i.get_name() << " " << i.get_value() << endl;
    // }

    my_pair<string, int> p1{"my1", 1};
    cout << p1.first << " " << p1.second << endl;

    my_pair<int, double> p2{200, 3.14};
    cout << p2.first << " " << p2.second << endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

默执_

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值