STL学习
手写STL源码模板
//TemplateDemo
#include<iostream>
using namespace std;
//交换两个变量
void MySwap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
//使用模板--自适应类型生成函数,地址不同
//函数重载和模板函数冲突,优先调用普通函数,或者使用<T>()显示调用
//不支持隐式转换
template<typename T>
void MyTSwap(T& a, T& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
MyTSwap(a, b);
MyPrint(a, b);
}操纵符重载
class Complex{public: double real; double image; Complex(double real, double image) :real(real), image(image) {} Complex Add(Complex& other) { return Complex(this->real + other.real, this->image + other.real); } Complex Mulitply(Complex& other) { double a = this->real; double b = this->image; double c = other.real; double d = other.image; return Complex(a * c - b * d, b * c + a * d); } Complex operator+(Complex& other) { return Complex(this->real + other.real, this->image + other.real); } Complex operator*(Complex& other) { double a = this->real; double b = this->image; double c = other.real; double d = other.image; return Complex(a * c - b * d, b * c + a * d); }};ostream& operator
页:
[1]