1、stack & operator=(stack & rightValue);/栈的容量增倍void stack:inflate()int index, *tp;tp = (int *)malloc(sizeof(int) * max * 2);for(index = 0; index top; index+)tpindex = spindex;max += max;free(sp);sp = tp;/出栈int stack:pop()if(top using namespace std;class Arrayint *a, len;void inflate() cout the array i
2、s inflating. endl; int *b = (int *)malloc(sizeof(int) * len * 2); for(int i = 0; i len; i+) bi = ai; len += len; free(a); a = b;Array(int size):len(size) a = (int *)malloc(sizeof(int) * size);int & operator(int index) if(index = len) inflate(); return aindex;void trans() ai time.h/求最小值template T1 &m
3、in(T1 *x, int n, T2 cmp)int j = 0;for(int i = 1; n; if(cmp(xi, xj) j = i;return xj;/函数对象class Fun_objbool operator()(int x, int y) return x y;int cmp(int x, int y)return x int a = 0, 1, 7, 9, 5, 4, -2, 3;int len = sizeof(a)/sizeof(int);Fun_obj fo;/cout min by function_object: / min(a, sizeof(a)/size
4、of(int), fo) min by function_pointer:/ min(a, sizeof(a)/sizeof(int), cmp) long num = 100000000;clock_t start, finish;double duration;start = clock(); num; min(a, len, fo);finish = clock();duration = (double)(finish - start); / / CLOCKS_PER_SEC;cout min by function_object : duration 的重载值类型 operator-(
5、) 函数体返回值必须是(1)指针 或 (2)类类型的对象或者对象的引用调用语法:object-member执行过程:(1)如果object是指针,所指对象需要有member成员;(2)object是一个对象或对象引用,所属类必须重载-计算-,得到obj(指针),做object = obj,转(1)。例如:class Aint x;A(int value):x(value)A * operator-() return this;class BA & a;B(A & value):a(value) operator- return a;A a(99);B b(a); b-x next; retur
6、n *this; operator+(int) return operator+(); bool operator!=(iter i) return p != i.p; Node * operator- return p;iter begin() return iter(this);iter end() return iter();void print() iter p = begin(); iter q = end(); while(p != q) p-info begin(), j = (*h)-end(); while(i != j & i- this-info) i+;next = i.p; if(i.q = NULL) *h = this; else i.q-next = this;Node * head = NULL;(new Node(2)-insert(&head);(new Node(3)-(new Node(1)-(new Node(4)-head-print();这里需要注意的是,在C+程序中,重载“+”和“-”运算符正确理解它们的语义很重要。int b = +a;语义:a += 1;int b = a;而int b = a+;语义是:int temp = a;int b = temp;temp.int();而不是
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1