ArrayBag<ItemType> ArrayBag<ItemType>::difference(const ArrayBag<ItemType> bag2) { for (int i=0; i<getCurrentSize(); i++) { if (bag2.contains(items[i])) { remove(items[i]); } } }
ArrayBag<ItemType> ArrayBag<ItemType>::intersection(const ArrayBag<ItemType> bag2) { ArrayBag<ItemType> interBag; for (int i=0; i<getCurrentSize(); i++) { if (bag2.contains(items[i])) { interBag.add(items[i]); } } return interBag; }
void bagTester(ArrayBag<string>& bag) { cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl; cout << bag; ifstream myfile ("Words.txt"); int currSize = 10; int count = 0; if (myfile.is_open()) { string line; while ( getline (myfile,line) ) { if (count < currSize) { cout << line << endl; bag.add(line); count++; } //else if (count == currSize) { // ArrayBag<string> temp(bag); // count++; //temp = move(bag); //} } } string numbers[] = { "one", "two", "three", "four", "five", "one", "six", "seven", "eight" }; cout << "Add items to the bag container: " << endl; for (int i = 0; i < sizeof(numbers)/sizeof(numbers[0]); i++) { bag.add( numbers[i] ); } // end for cout << bag; cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 0 (false)" << endl; cout << "getCurrentSize: returns " << bag.getCurrentSize() << "; should be 9" << endl; cout << "Try to add another entry: add(\"nine\") returns " << bag.add("nine") << endl; cout << "contains(\"three\"): returns " << boolalpha << bag.contains("three") << "; should be 1 (true)" << endl; cout << "contains(\"ten\"): returns " << boolalpha << bag.contains("ten") << "; should be 0 (false)" << endl; cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 2" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl; cout << "getFrequencyOf(\"one\"): returns " << bag.getFrequencyOf("one") << " should be 1" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 1 (true)" << endl; cout << "remove(\"one\"): returns " << boolalpha << bag.remove("one") << "; should be 0 (false)" << endl; cout << endl; cout << bag; cout << "After clearing the bag, "; bag.clear(); cout << "isEmpty: returns " << boolalpha << bag.isEmpty() << "; should be 1 (true)" << endl; } // end bagTester