cs32homework&projects

cs32homework&projects - Homework 1 Time due: 9:00...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Homework 1 Time due: 9:00 PM Tuesday, April 14 Here is a C++ class definition for an abstract data type Multiset of string s, representing the concept of an unordered collection of strings with duplicates allowed. (A web server might record visits to a website in a multiset of strings, for example: Each time someone visits the site, a string identifying the visitor is stored in the multiset. The same visitor may visit multiple times.) To make things simpler for you, the case of letters in a string matters, so that the strings cumin and cUmIn are not considered duplicates. class Multiset { public: Multiset(); // Create an empty multiset. bool empty(); // Return true if the multiset is empty, otherwise false. int size(); // Return the number of items in the multiset. For example, the size // of a multiset containing "cumin", "cumin", "cumin", "turmeric" is 4. int uniqueSize(); // Return the number of distinct items in the multiset. For example, // the uniqueSize of a multiset containing "cumin", "cumin", "cumin", // "turmeric" is 2. bool insert(const std::string& value); // Insert value into the multiset. Return true if the value was // actually inserted. Return false if the value was not inserted // (perhaps because the multiset has a fixed capacity and is full). int erase(const std::string& value); // Remove one instance of value from the multiset if present. // Return the number of instances removed, which will be 1 or 0. int eraseAll(const std::string& value); // Remove all instances of value from the multiset if present. // Return the number of instances removed. bool contains(const std::string& value); // Return true if the value is in the multiset, otherwise false. int count(const std::string& value); // Return the number of instances of value in the multiset. int get(int i, std::string& value); // If 0 <= i < uniqueSize(), copy into value an item in the // multiset and return the number of instances of that item in // the multiset. Otherwise, leave value unchanged and return 0. // (See below for details about this function.) void swap(Multiset& other); // Exchange the contents of this multiset with the other one. }; The get function enables a client to iterate over all elements of a Multiset because of this property it must have: If nothing is inserted into or erased from the multiset in the interim, then calling get repeatedly with the first parameter ranging over all the integers from to uniqueSize()-1 inclusive will copy into the second parameter every value from the multiset exactly once. The order in which elements are copied is up to you. In other words, this code fragment Multiset ms; ms.insert("fennel"); ms.insert("fennel"); ms.insert("fenugreek"); ms.insert("fennel"); for (int k = 0; k < ms.uniqueSize(); k++) { string x; int n = ms.get(k, x); cout << x << " occurs " << n << " times." << endl; } must write either fennel occurs 3 times....
View Full Document

Page1 / 65

cs32homework&projects - Homework 1 Time due: 9:00...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online