Set.h
#include
#include
#include
using namespace std;
#ifndef Set_H
#define Set_H
//定义适合本文的集合(Set)抽象数据类型--------------------------------------------
class Set
{
public:
Set():vec(0){};
Set(int A[], int n);
//用数组初始化集合
Set(const Set &s);
//用集合初始化集合
Set(vector v);
const Set& operator=(const Set& s); //集合赋值运算符重载
bool IsEmpty();
//判断集合是否为空集
int Card();
//计算集合 A 的模
void Clear();
//集合清空
int Find(int a);
//在集合中查找元素 a,返回位置,不存在返回-1
int KeyAt(int i);
//返回集合中 i 位置的 key
void SetKey(int index, int Val); //设置 index 位置的值为 Val
void Sort();
//对集合中的元素按从小到大排序
void Remove(int a);
//在集合中删除元素 a 得到新的集合
void Add(int a);
//在集合中添加元素 a 得到新的集合
bool BelongTo(Set &s);
//判断集合 A 是否属于集合 B
bool operator==(Set &s);
//判断两个集合是否相等
Set Sub(Set &s)const;
//集合 A 减 B 计算
Set Union(Set &s)const;
//集合 A 并 B 计算
Set
InterSect(Set &s)const; //集合 A 交 B 的计算
Set ValueOfAttribute();
//求属性 a 值集的值域
vector ToIntVector();
//把该集合转化成整型向量
void Print();
//在屏幕打印集合 A
private:
vector vec;
};
#endif
Set.cpp
#include"Set.h"
//集合类 Set 的实现-----------------------------------------------
Set::Set(int A[], int n)
{
for(int i=0; i