用c++编码 定义一个集合类setColour,要求元素为枚举类型值。例如,enum colour { red, yellow, blue, white, black };集合类实现交、并、差、属于、蕴含、输入、输出等各种基本运算。设计main函数测试setColour类的功能。
时间: 2024-05-01 15:20:14 浏览: 76
以下是一个基本的setColour类的实现,实现了集合的基本运算,包括交、并、差、属于、蕴含、输入、输出等:
```c++
#include <iostream>
#include <set>
enum Colour {
red,
yellow,
blue,
white,
black
};
class setColour {
public:
setColour() = default;
setColour(std::initializer_list<Colour> colours) : colours(colours) {}
setColour(const setColour& other) : colours(other.colours) {}
setColour& operator=(const setColour& other) {
if (this != &other) {
colours = other.colours;
}
return *this;
}
setColour operator&(const setColour& other) const {
setColour result;
std::set_intersection(colours.begin(), colours.end(), other.colours.begin(), other.colours.end(), std::inserter(result.colours, result.colours.begin()));
return result;
}
setColour operator|(const setColour& other) const {
setColour result;
std::set_union(colours.begin(), colours.end(), other.colours.begin(), other.colours.end(), std::inserter(result.colours, result.colours.begin()));
return result;
}
setColour operator-(const setColour& other) const {
setColour result;
std::set_difference(colours.begin(), colours.end(), other.colours.begin(), other.colours.end(), std::inserter(result.colours, result.colours.begin()));
return result;
}
bool operator==(const setColour& other) const {
return colours == other.colours;
}
bool operator!=(const setColour& other) const {
return colours != other.colours;
}
bool operator<=(const setColour& other) const {
return std::includes(other.colours.begin(), other.colours.end(), colours.begin(), colours.end());
}
bool operator>=(const setColour& other) const {
return std::includes(colours.begin(), colours.end(), other.colours.begin(), other.colours.end());
}
bool operator<(const setColour& other) const {
return colours < other.colours;
}
bool operator>(const setColour& other) const {
return colours > other.colours;
}
friend std::ostream& operator<<(std::ostream& os, const setColour& set) {
os << "{ ";
for (auto c : set.colours) {
os << c << " ";
}
os << "}";
return os;
}
friend std::istream& operator>>(std::istream& is, setColour& set) {
Colour colour;
while (is >> colour) {
set.colours.insert(colour);
}
return is;
}
private:
std::set<Colour> colours;
};
int main() {
setColour set1{red, yellow, blue};
setColour set2{yellow, white, black};
setColour set3{red, yellow, blue};
std::cout << "Set 1: " << set1 << std::endl;
std::cout << "Set 2: " << set2 << std::endl;
std::cout << "Set 3: " << set3 << std::endl;
std::cout << "Set 1 & Set 2: " << (set1 & set2) << std::endl;
std::cout << "Set 1 | Set 2: " << (set1 | set2) << std::endl;
std::cout << "Set 1 - Set 2: " << (set1 - set2) << std::endl;
std::cout << "Set 1 == Set 2: " << (set1 == set2) << std::endl;
std::cout << "Set 1 != Set 2: " << (set1 != set2) << std::endl;
std::cout << "Set 1 <= Set 2: " << (set1 <= set2) << std::endl;
std::cout << "Set 1 >= Set 2: " << (set1 >= set2) << std::endl;
std::cout << "Set 1 < Set 2: " << (set1 < set2) << std::endl;
std::cout << "Set 1 > Set 2: " << (set1 > set2) << std::endl;
std::cout << "Enter a set of colours: ";
setColour set4;
std::cin >> set4;
std::cout << "Set 4: " << set4 << std::endl;
return 0;
}
```
在main函数中,我们定义了三个集合set1、set2、set3,分别包含了不同的元素。然后进行了交、并、差等基本运算,并输出结果。然后又测试了集合相等、包含等关系的运算符。最后,我们通过输入流运算符重载,从控制台输入一个集合,并输出结果。
阅读全文