STL finish 集合交、差、并、补运算
2015-10-26 18:35
204 查看
分析
集合的运算嘛,使用链表啊;YOU are right. 不过,最近学了STL模板库,可不可以用模板做呢?YES.而且很容易想到STL set.why
set模板拥有强大的自动排序功能,减化排序与去重操作think,不需要你编写冗杂的排序与去重,是不是可以节约很多时间呢?
节省了时间,赶紧去霸占篮球场啊(嘿嘿,我是詹皇的球迷哦)
Follow is my code in details
#include <iostream> #include <cstdlib> #include <windows.h> #include <set> using namespace std; set<char> Collection_Quan; set<char> Collection_A; set<char> Collection_B; void Menu(); void Set_Q(); void Set_A(); void Set_B(); void Set_Jihe(); void Bingji();//并集 void Jiaoji();//交集 template <class T> void Chaji(const T&, const T&); //差集 template <class T> void show(const T& Input)//T是迭代器 { for(auto im = Input.cbegin();im != Input.cend();im++) cout << *im << " "; cout << endl; } int main() { system("color 0E"); cout << "程序功能求集合A与B的交集、并集、差集、补集" << endl; Set_Jihe(); Menu(); //Bingji(); //Jiaoji(); //Chaji(); system("pause"); return 0; } void Bingji()//求A 与 B 并集 { set <char> bingji; bingji.insert(Collection_A.begin(), Collection_A.end()); bingji.insert(Collection_B.begin(), Collection_B.end()); cout << "A 并 B = : "; show(bingji); } void Jiaoji()//求A 与 B 交集 { set<char> jiaoji; for(auto iElem = Collection_A.begin();iElem != Collection_A.end();iElem++) for(auto jElem = Collection_B.begin();jElem != Collection_B.end();jElem++) if(*iElem == *jElem) { jiaoji.insert(*iElem); break; } cout << "A 交 B = : "; show(jiaoji); } template <class T> void Chaji(const T& First_Co, const T& Second_Co)//求A 与 B 差集 { set<char> chaji; for(auto iElem = First_Co.cbegin();iElem != First_Co.cend();iElem++) { bool flag = true; for(auto jElem = Second_Co.cbegin();jElem != Second_Co.cend();jElem++) if(*iElem == *jElem)//找出A中存在, B中不存在的元素 { flag = false; break; } if(flag) chaji.insert(*iElem); } if(First_Co == Collection_A) cout << "A - B = : "; else cout << "B - A = : "; show(chaji); } void Menu() { cout << "***求集合A与B的交,并、补集***" << endl; cout << "1.交集 2.并集 3.A - B 4.B- A 5.退出" << endl; cout << "******************************" << endl; cout << "choose :" << endl; int n; while(1) { cin >> n; switch(n) { case 1: Jiaoji();break; case 2: Bingji();break; case 3: Chaji(Collection_A, Collection_B); break;//A - B case 4: Chaji(Collection_B, Collection_A); break;//B - A case 5: exit(1); default:break; } } } void Set_Jihe() { Set_Q(); Set_A(); Set_B(); } void Set_Q() { char Elem_x; int Q_count; auto cheak = Q_count; cout << "请输入全集Q包含元素的个数:"; cin >> cheak; while(sizeof(cheak) != sizeof(int)) { cout << "输入错误,请重新输入: " << endl ;//此处要有错误处理,判断 cin >> cheak; } Q_count = cheak; cout << "请输入全集Q包含元素值: "; while(Q_count--) { cin >> Elem_x; Collection_Quan.insert(Elem_x); } cout << endl; } void Set_A() { char Elem_x; int A_count; cout << "请输入集合A包含元素的个数:"; cin >> A_count; cout << "请输入集合A包含元素值: "; while(A_count--) { cin >> Elem_x; Collection_A.insert(Elem_x); } cout << endl; } void Set_B() { char Elem_x; int B_count; cout << "请输入集合B包含元素的个数:"; cin >> B_count; cout << "请输入集合B的元素值: "; while(B_count--) { cin >> Elem_x; Collection_B.insert(Elem_x); } cout << endl; } //if the code is terrible, I hope you can tell me. :)
Be quick to finish your code amzingly.
HERE 詹皇 :)
哈哈,当然是要写完成code tasks nuo.加油.
相关文章推荐
- [C/C++]反转链表
- C#实现基于链表的内存记事本实例
- 修复mysql数据库
- nodejs中的fiber(纤程)库详解
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++实现简单的学生管理系统
- 桌面中心(一)创建数据库
- c++ STL容器总结之:vertor与list的应用
- Linux内核链表实现过程
- C++链表倒序实现方法
- C++在成员函数中使用STL的find_if函数实例
- C#通过链表实现队列的方法
- C#实现的简单链表类实例
- 浅析C语言头文件和库的一些问题
- 找出链表倒数第n个节点元素的二个方法
- 桌面中心(三)修改数据库
- 目前流行的JavaScript库的介绍及对比
- Java数据结构之简单链表的定义与实现方法示例