Guest User

Untitled

a guest
Jan 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <set>
  3. #include <list>
  4.  
  5.  
  6. /*function that retuns a list of all the n-elm subsets of a given set
  7. if the given set is [1,2,3] and n is 2, then the returned list should contain [1,2], [1,3], and
  8. [2,3]. The order of the elements is not important.*/
  9. template <typename T>
  10. std::list<std::set<T>> subSet(std::set<T> &s, int num){
  11. std::list<T> tempList;
  12. std::set<T> tmpSet;
  13. std::set <T> :: iterator iterFront, iterNext;
  14. if (num==0)
  15. tmpList.insert(tmpSet);//emptyList
  16.  
  17. else if (num==1){ //copy the set as a list
  18. while(iterFront!=s.end()){
  19. tmpList.insert(*iterFront); //copies combinations in subsets [num,num2]
  20. iterFront++;
  21. }
  22. }
  23.  
  24. else if (num>1){
  25. iterFront=s.begin(); //points to first elm, (1,2)...(1 3) (2 2)
  26. iterNext=iterFront;
  27. while (iterFront!=s.end()){
  28. while(iterNext!=s.end()){
  29. for (int i=0; i<num; i++){
  30. tmpSet.insert(*iterFront); //copies combinations in subsets [num,num2]
  31. tmpSet.insert(*(iterNext+1));
  32. tempList.insert(subSet(s),i); //inserts each subset into list
  33. }
  34. iterNext++; //move on to the next iterator
  35. }
  36. s.erase(iterFront);//removes first elm since all is copied with first elm in it
  37. iterFront++;
  38. }
  39. }
  40. return tempList;
  41. }
  42.  
  43. template <typename T>
  44. void printList(std::list<T> &argList){
  45. std::list<T>::iterator iter;
  46. for (iter=argList.begin(); iter<argLst.end(); iter++)
  47. std::cout << *iter << " ";
  48. }
  49.  
  50. int main(){
  51. int n, int num;
  52. std::set <int> s;
  53. std::list <set<int>> returnList;
  54. std::cout << "Enter set.size() : "
  55. std::cin >> n;
  56.  
  57. for (int i=0; i<n; i++)
  58. s.insert(i); //numbers 1...n
  59.  
  60. std::cout << "Enter num : "
  61. std::cin >> num;
  62.  
  63. returnList=subset(s,num);
  64. printList(returnList);
  65. }
Add Comment
Please, Sign In to add comment