好久没更了,摆还是爽
遗留问题:
(16条消息) int&作为函数返回类型-编程语言-CSDN问答(已解决)
宏:
预处理器编译指令都以#打头
#define(宏常量)使得预处理器进行文本替换,而不考虑其他,编译器将检查文本正确与否。 不解风情
宏需要大量括号,凡变量尽量都打一括号。
列如:
#define i* int*
i* a,b;
此时我们的本意是   int *a,*b;
而实际上的代码为   int *a, b;
此时的 b 为 int 型,而非 int* 型使用宏避免多次包含
reference:
(6条消息) #ifndef,#define,#endif的作用和用法_Junzheng Li的博客-CSDN博客
(7条消息) #ifndef/#define/#endif使用详解_ 司 的博客-CSDN博客_#ifndef _a_h_
例:

assert宏:验证表达式
语法:
assert (expression that evaluates to true or false);包含:于<assert.h>头文件
示例:
#include <assert.h> 
int main() 
{ 
 char* sayHello = new char [25]; 
 assert(sayHello != NULL); // throws a message if pointer is NULL 
 // other code 
 delete [] sayHello; 
 return 0; 
}在我的DEV编译器中出现如下报错:

注:
宏不支持任何形式的类型安全,这是一个严重的缺点。另外,复杂的宏调试起来也不容易

模板:
template:n. 样板;模板;型板
声明模板以关键字template打头
//模板类,使用需要<type>
template <typename T/*可多写*/> 
class HoldVarTypeT 
{ 
private: 
 T value; 
public: 
 void SetValue (const T& newValue) { value = newValue; } 
 T& GetValue() {return value;} 
};instantiation :实例化
template <typename T1, typename T2> 
class HoldsPair 
{ 
private: 
 T1 value1; 
 T2 value2; 
public: 
 // Constructor that initializes member variables 
 HoldsPair (const T1& val1, const T2& val2) 
 { 
 value1 = val1; 
 value2 = val2; 
 }; 
 // ... Other member functions 
};实例化:
HoldsPair <int, double> pairIntDouble (6, 1.99);
HoldsPair <int, int> pairInts (6, 500);template <typename T1=int, typename T2=int>
class HoldsPair
{
// ... method declarations
};这样,前述第二种 HoldsPair 用法可以简写为:
// Pair an int with an int (default type)
HoldsPair <> pairInts (6, 500);#include <iostream>
using namespace std;
template <typename T1 = double, typename T2 = int >
class HoldsPair {
	private:
		T1 value1;
		T2 value2;
	public:
		HoldsPair(const T1& val1, const T2& val2) // constructor
			: value1(val1), value2(val2) {}
		// Accessor functions
		const T1& GetFirstValue() const {
			cout << "fanhui double " << value1 << endl;
			return value1;
		}
		const T2& GetSecondValue() const;
		//如果这两个函数不给出定义,则无返回,出现return 1;的报错
};
// specialization of HoldsPair for types int & int here
template<> class HoldsPair<int, int> {
	private:
		int value1;
		int value2;
		string strFun;
	public:
		HoldsPair(const int& val1, const int& val2) // constructor
			: value1(val1), value2(val2) {}
		const int & GetFirstValue() const {
			cout << "Returning integer " << value1 << endl;
			return value1;
		}
};
int main() {
	HoldsPair<int, int> pairIntInt(222, 333); //在类型为int,int时调用第二个,否则调用第一个
	pairIntInt.GetFirstValue();
	return 0;
}模板类的具体化,如上。
示例代码:
#include <iostream>
using namespace std;
template <typename T>
class TestStatic {
	public:
		static int staticVal;
};
// static member initialization
template<typename T> int TestStatic<T>::staticVal;
int main() {
	TestStatic<int> intInstance;
	cout << "Setting staticVal for intInstance to 2011" << endl;
	intInstance.staticVal = 2011;
	TestStatic<double> dblnstance;
	cout << "Setting staticVal for Double_2 to 1011" << endl;
	dblnstance.staticVal = 1011;
	cout << "intInstance.staticVal = " << intInstance.staticVal << endl;
	cout << "dblnstance.staticVal = " << dblnstance.staticVal << endl;
	return 0;
}
Q:静态变量有啥用?
template <typename T1, typename T2, typename T3>
void Sum(T1& result, T2 num1, T3 num2)
{
result = num1 + num2;
return;
}
 想要加上不同的值应如下进行编码:参变量可变的模板
#include<iostream>
using namespace std;
template<typename R,typename V>
void Sum(R& result,V& val){
	result+=val;
	cout<<"i have in prime"<<endl;
}
template<typename R, typename First, typename...Rest>
void Sum(R& result, First val1, Rest...valN) {
	result += val1;
	return Sum(result, valN...);
}
int main() {
	double dResult = 0;
	Sum(dResult, 3.14, 4.56, 1.1111);
	cout<<"dResult"<<dResult<<endl;
	
} 注:必须要有基础模板
注:必须要有基础模板
引用:(3条消息) C++的get()函数使用详解_Nine_CC的博客-CSDN博客_c++ get
使用static_assert();
static_assert(expression being validated, "Error message when check fails");static_assert(sizeof(T) != sizeof(int), "No int please!");如图:

DEV编译器报错为:


编写模板函数Display,它可调用不同数量和类型的的参数并显示。
#include<iostream>
using namespace std;
template<typename t0>
void Display(t0& e0) {
	cout << "different from xiamian de , i will be used when the en is used\
 that's to say , the element en won't be used int this case" << endl;
}
template<typename t0,  typename ...other>
void Display(t0& e0, other...en) {
	cout << e0 << endl;
	return Display(en...);
}
int main() {
	string a = "fu huo ba", b = "wo";
	string c = "de", d = "ai", f = "ren";
	Display(a, b, c, d, f, "hhh");
	return 0;
}
运行结果:

一版和上面稍有不同的代码:
#include<iostream>
using namespace std;
void Display() {
	cout << "i will be use at last,when all the elements are use\
 even Display(en)" << endl;
}
template<typename t0,  typename ...other>
void Display(t0& e0, other...en) {
	cout << e0 << endl;
	Display(en...);
}
int main() {
	string a = "fu huo ba", b = "wo";
	string c = "de", d = "ai", f = "ren";
	Display(a, b, c, d, f, "hhh");
	return 0;
}
运行结果:














![[RoarCTF 2019]Online Proxy(x-forwarded-for盲注)](https://img-blog.csdnimg.cn/6723c9beff2c48398cedca1fad58701c.png)





