宏定义
#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;
}