#include
using namespace std;
template
class myclass
{
T * b;
int size;
int capcity;
public:
void push(T x)
{
// 你的程序没有分配内存
if(capcity == size)
{
if(capcity == 0)
{
capcity = 10;
b = new T[capcity+1];
}
else
{
capcity *= 2;
T * n = new T[capcity+1];
memcpy(n, b, sizeof(T) * size);
delete [] b;
b = n;
}
}
b[size] = x;
size++;
}
myclass(T * p = NULL)
{
b = p;
size = 0;
capcity = 0;
}
T sum();
bool find(T x);
void display()
{
T * p = b;
T * ph = p + size;
while(p < ph) cout << *p++ << " ";
cout << endl;
}
};
template
T myclass::sum()
{
T sum = 0;
T * p = b;
T * ph = p + size;
while(p < ph) sum += *p++;
return sum;
}
template
bool myclass::find(T x)
{
T * p = b;
T * ph = p + size;
while(p < ph)
{
T m = *p++ - x;
if(-0.0000001 < m && m < 0.0000001 )
{
cout << (size - (ph - p) - 1) << " ";
return true;
}
}
return false;
}
int main()
{
myclass a1;
myclass a2;
myclass a3;
for(int i = 1;i <= 100; i++)
{
a1.push(i);
a2.push((double)i);
a3.push((float)i);
}
a1.display();
cout << "find 50 frome a1 is " << a1.find(50) << endl;
cout << "find 50 frome a2 is " << a2.find(50) << endl;
cout << "find 50 frome a3 is " << a3.find(50) << endl;
cout << "the sum of a1 is "<< a1.sum() << endl;
cout << "the sum of a2 is "<< a2.sum() << endl;
cout << "the sum of a3 is "<< a3.sum() << endl;
system("pause");
return 0;
}