C++[クラス設計]newダイナミックメモリ割り当てを使用する集合クラス
2412 ワード
整数集合クラスint_を定義して実装するset,コレクションクラスにおけるcur_sizeは、現在のコレクションにいくつかの整数があることを示し、コレクションにはmax_が最も多く含まれています.size個の整数で、集合要素を格納する配列は動的です.要求される方法は次のとおりです.
(1)集合に整数を追加する.
(2)集合から要素を1つ取り除く.
(3)1つの要素が集合中であるか否かを判断する.
(4)重荷重<>演算子出力セット;
(5)集合の交差,並列,差演算をそれぞれ実現する.(未実装)
(1)集合に整数を追加する.
(2)集合から要素を1つ取り除く.
(3)1つの要素が集合中であるか否かを判断する.
(4)重荷重<>演算子出力セット;
(5)集合の交差,並列,差演算をそれぞれ実現する.(未実装)
//in int_set.h
#ifndef _INT_SET_H_
#define _INT_SET_H_
#include
#include
#define my_max_size INT_MAX
class int_set
{
private:
int * m_pInt;
public:
int cur_size;
public:
int_set();
bool AddEle(int Element);
bool IsInSet(int CheckEle);
bool RemoveEle(int RemElement);
int At(int index);
inline int * GetpInt() const {return m_pInt;}
void OutPut();
int_set & operator << (int element);
friend std::ostream & operator >> (std::ostream & os,const int_set & set);
~int_set();
};
#endif
//in int_set.cpp
#include "int_set.h"
#include
int_set::int_set()
{
m_pInt=NULL;
cur_size=0;
}
bool int_set::AddEle(int Element)
{
if(cur_size==my_max_size)
return false;
int * pIntNew=new int[cur_size+1];
if(m_pInt != NULL)
{
/*int nErr=memcpy_s(pIntNew,sizeof(int)*(cur_size+1),m_pInt,sizeof(int)*(cur_size));
if(nErr !=0)
{
delete []pIntNew;
return false;
}*/
for(int i=0;iAddEle(element);
return *this;
}
std::ostream & operator >> (std::ostream & os,const int_set & set)
{
//set.OutPut();Can not do this,because >> is not a member function.
for(int i=0;i> MySet;
//cout << "cur_size:" << MySet.cur_size << endl;
//MySet.RemoveEle(7);//OK
//MySet.OutPut();//OK
if(MySet.IsInSet(8))
cout << MySet.At(7) << endl;
}