64,656
社区成员
发帖
与我相关
我的任务
分享
lthyxy: VC6 对C++标准支持得不好,听taodm的吧,远离它
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
template<class M> friend ostream& operator <<(ostream& outs,const List<M> & the_list);
void erase();
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
template<class M>
ostream& operator <<(ostream& outs,const List<M> & the_list)
{
//重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0; i<the_list.current_length; i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
#include <iostream>
#include <cstdlib>
using namespace std;
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
template<class T> friend ostream& operator <<(ostream& outs,const List<T> & the_list);
void erase();
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
template<class T>
ostream& operator <<(ostream& outs,const List<T> & the_list)
{
//重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0; i<the_list.current_length; i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
template<class T>
List<T>::List(int max):max_length(max),current_length(0)
{
item=new T[max];
}
template<class T>
List<T>::~List()
{
delete [] item;
}
template<class T>
int List<T>::length() const
{
return current_length;
}
template<class T>
void List<T>::add(T new_item)
{
if(full())
{
cout<<"error:adding to a full list.";
exit(1);
}
else
{
item[current_length]=new_item;
current_length=current_length+1;
}
}
template<class T>
bool List<T>::full() const
{
return current_length==max_length;
}
template<class T>
void List<T>::erase()
{
current_length=0;
}
//主函数体内容如下:
#include "list.h"
int main()
{
List<int> first_list(2);
first_list.add(1);
first_list.add(2);
cout<<"first list = \n"<<first_list;
List<char> second_list(10);
second_list.add('A');
second_list.add('B');
second_list.add('C');
cout<<"second list = \n"<<second_list;
return 0;
}
friend ostream& operator<< <T>(ostream& outs,const List<T>& the_list);
#include <iostream>
#include <cstdlib>
using namespace std;
template<class T>
class List
{
public:
List(int max);
~List();
int length() const;
void add(T new_item);
bool full() const;
template<class M>
friend ostream& operator <<(ostream& outs,const List<M> & the_list);
void erase();
private:
T *item; //列表的首地址
int max_length;
int current_length;
};
template<class M>
ostream& operator <<(ostream& outs,const List<M> & the_list)
{
//重载<<运算符,用<<运算符输出列表各元素的值
for(int i=0; i<the_list.current_length; i++)
{
outs<<the_list.item[i]<<endl;
}
return outs;
}
template<class T>
List<T>::List(int max):max_length(max),current_length(0)
{
item=new T[max];
}
template<class T>
List<T>::~List()
{
delete [] item;
}
template<class T>
int List<T>::length() const
{
return current_length;
}
template<class T>
void List<T>::add(T new_item)
{
if(full())
{
cout<<"error:adding to a full list.";
exit(1);
}
else
{
item[current_length]=new_item;
current_length=current_length+1;
}
}
template<class T>
bool List<T>::full() const
{
return current_length==max_length;
}
template<class T>
void List<T>::erase()
{
current_length=0;
}
//主函数体内容如下:
#include "list.h"
int main()
{
List<int> first_list(2);
first_list.add(1);
first_list.add(2);
cout<<"first list = \n"<<first_list;
List<char> second_list(10);
second_list.add('A');
second_list.add('B');
second_list.add('C');
cout<<"second list = \n"<<second_list;
return 0;
}
/*
first list =
1
2
second list =
A
B
C
Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.
*/