



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




map<string, vector
"hello"^15
first second
p
q
map<string, vector
m[string("hello")].push_back(5);
p = m.find(string("hello")); p->second.size()
(p->second)[i] = 15;
vector
15.2 Exercise
map<string, vector
15.3 A Word Index in a Text File
// Given a text file, generate an alphabetical listing of the words in the file // and the file line numbers on which each word appears. If a word appears on // a line more than once, the line number is listed only once. #include
// implementation omitted, will be covered in a later lecture vector
int main() { map<string, vector
while (getline(cin, line)) { line_number++; // Break the string up into words vector
// Find if each word is already in the map. for (vector
// Output each word on a single line, followed by the line numbers. map<string, vector
15.7 MP3 class
class MP3 { public: // CONSTRUCTORS MP3() {} MP3(const string& artist, const string& title) : artist_(artist), title_(title) {} // ACCESSORS const string& get_artist() const { return artist_; } const string& get_title() const { return title_; } private: // REPRESENTATION string artist_; string title_; };
15.8 Computer class (implementation omitted)
class Computer { public: // CONSTRUCTOR Computer(double speed) : speed_(speed) { }
// Iterate through the list of MP3, printing each. void print_mp3s() const; // Returns true if the Computer has the requested song, returns reference to song via parameter bool has_song(const string& artist, const string& title, MP3& requested_song) const; // Build a list of all songs by the indicated artist. list
// Add an MP3 to the list of MP3s. Return false if the song is already there. bool add_mp3(const string& artist, const string& title); private: // REPRESENTATION double speed_; // network connection speed in bytes per second list
15.9 Exercise: Understanding the data structure
15.10 main.cpp (some parsing omitted)
// Each computer is identified by a unique id (a string) typedef map<string, Computer> MP3_map;
int main() { MP3_map mp3_system; bool quit = false; char option; while (cin >> option) { switch (option) { case ’a’: add_computer(mp3_system); break; case ’p’: print_mp3s_on_computer(mp3_system); break; case ’f’: find_song(mp3_system); break; case ’F’: find_songs_by_artist(mp3_system); break; case ’q’: return 1; break; default: cout << "Error: " << option << " is not a valid command" << endl;; break; } } }
// Add a computer to the map of MP3’s void add_computer(MP3_map& mp3_system) { // READS IN: computer_id (string), speed (double), // and artist (string) & title (string) for all songs
// Try to add the computer using the map insert function. pair<MP3_map::iterator, bool> result_pair = mp3_system.insert(make_pair(computer_id, new_computer)); assert(result_pair.second); // make sure the computer isn’t already there! cout << "Computer " << computer_id << " added." << endl; }
// Print the MP3’s for a given computer. void print_mp3s_on_computer(const MP3_map& mp3_system) { string computer_id; cin >> computer_id; MP3_map::const_iterator c_itr = mp3_system.find(computer_id); if (c_itr == mp3_system.end()) { cout << "Computer " << computer_id << " is not in the system." << endl; } else { cout << "Songs found on " << computer_id << ":\n"; c_itr->second.print_mp3s(); } }
// Find all computers with a particular song. void find_song(const MP3_map& mp3_system) { string artist, title; cin >> artist >> title; bool found = false; string computer_id; double speed; // Iterate through the map, looking for the song. MP3_map::const_iterator c_itr; for (c_itr = mp3_system.begin(); c_itr != mp3_system.end(); c_itr++) { MP3 request; if (c_itr->second.has_song(artist, title, request)) { if (!found || c_itr->second.getSpeed() > speed) { found = true; computer_id = c_itr->first; speed = c_itr->second.getSpeed(); } }