Docsity

CSCI-1200 Computer Science II - Lecture 15: Associative Containers (Maps) Part 2, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CSCI-1200 Computer Science II — Fall 2007
Lecture 15 – Associative Containers (Maps), Part 2
Review of Lecture 14
•Maps are associations between keys and values.
•Maps have fast insert, access and remove operations: O(log n), we’ll learn why later in the term when we study
the implementation!
•Maps store pairs; map iterators refer to these pairs.
•The primary map member functions we discussed are operator[],find,insert, and erase.
•The choice between maps, vectors and lists is based on naturalness, ease of programming, and efficiency of the
resulting program.
Today’s Class — Maps, Part 2
•Maps containing more complicated values.
•Example: index mapping words to the text line numbers on which they appear.
•Maps whose keys are class objects.
•Example: maintaining student records.
•Summary discussion of when to use maps.
15.1 More Complicated Values
map<string, vector<int> > m
15 5
"hello"
first second
p
q
•Let’s look at the example:
map<string, vector<int> > m;
map<string, vector<int> >::iterator p;
Note that the space between the > > is required.
Otherwise, >> is treated as an operator.
•Here’s the syntax for entering the number 5
in the vector associated with the string "hello":
m[string("hello")].push_back(5);
•Here’s the syntax for accessing the size of the vector stored
in the map pair referred to by map iterator p:
p = m.find(string("hello"));
p->second.size()
Now, if you want to access (and change) the ith entry in this vector you can either using subscripting:
(p->second)[i] = 15;
(the parentheses are needed because of precedence) or you can use vector iterators:
vector<int>::iterator q = p->second.begin() + i;
*q = 15;
Both of these, of course, assume that at least i+1 integers have been stored in the vector (either through the
use of push back or through construction of the vector).
•We can figure out the correct syntax for all of these by drawing pictures to visualize the contents of the map
and the pairs stored in the map. We will do this during lecture, and you should do so all the time in practice.
pf3
pf4
pf5

Partial preview of the text

Download CSCI-1200 Computer Science II - Lecture 15: Associative Containers (Maps) Part 2 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CSCI-1200 Computer Science II — Fall 2007

Lecture 15 – Associative Containers (Maps), Part 2

Review of Lecture 14

  • Maps are associations between keys and values.
  • Maps have fast insert, access and remove operations: O(log n), we’ll learn why later in the term when we study

the implementation!

  • Maps store pairs; map iterators refer to these pairs.
  • The primary map member functions we discussed are operator[], find, insert, and erase.
  • The choice between maps, vectors and lists is based on naturalness, ease of programming, and efficiency of the

resulting program.

Today’s Class — Maps, Part 2

  • Maps containing more complicated values.
  • Example: index mapping words to the text line numbers on which they appear.
  • Maps whose keys are class objects.
  • Example: maintaining student records.
  • Summary discussion of when to use maps.

15.1 More Complicated Values

map<string, vector > m

"hello"^15

first second

p

q

  • Let’s look at the example:

map<string, vector > m; map<string, vector >::iterator p;

Note that the space between the > > is required.

Otherwise, >> is treated as an operator.

  • Here’s the syntax for entering the number 5

in the vector associated with the string "hello":

m[string("hello")].push_back(5);

  • Here’s the syntax for accessing the size of the vector stored

in the map pair referred to by map iterator p:

p = m.find(string("hello")); p->second.size()

Now, if you want to access (and change) the ith^ entry in this vector you can either using subscripting:

(p->second)[i] = 15;

(the parentheses are needed because of precedence) or you can use vector iterators:

vector::iterator q = p->second.begin() + i; *q = 15;

Both of these, of course, assume that at least i+1 integers have been stored in the vector (either through the

use of push back or through construction of the vector).

  • We can figure out the correct syntax for all of these by drawing pictures to visualize the contents of the map

and the pairs stored in the map. We will do this during lecture, and you should do so all the time in practice.

15.2 Exercise

Write code to count the odd numbers stored in the map

map<string, vector > m;

This will require testing all contents of each vector in the map. Try writing the code using subscripting on the vectors

and then again using vector iterators.

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 #include #include #include #include #include using namespace std;

// implementation omitted, will be covered in a later lecture vector breakup_line_into_strings(const string& line);

int main() { map<string, vector > words_to_lines; string line; int line_number = 0;

while (getline(cin, line)) { line_number++; // Break the string up into words vector words = breakup_line_into_strings(line);

// Find if each word is already in the map. for (vector::iterator p = words.begin(); p!= words.end(); ++p) { // If not, create a new entry with an empty vector (default) and // add to index to the end of the vector map<string, vector >::iterator map_itr = words_to_lines.find(p); if (map_itr == words_to_lines.end()) words_to_lines[p].push_back(line_number); // could use insert here // If it is, check the last entry to see if the line number is // already there. If not, add it to the back of the vector. else if (map_itr->second.back() != line_number) map_itr->second.push_back(line_number); } }

// Output each word on a single line, followed by the line numbers. map<string, vector >::iterator map_itr; for (map_itr = words_to_lines.begin(); map_itr != words_to_lines.end(); map_itr++) { cout << map_itr->first << ":\t"; for (unsigned int i = 0; i < map_itr->second.size(); ++i) cout << (map_itr->second)[ i ] << " "; cout << "\n"; } return 0; }

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 songs_by_artist(const string& artist) const; double getSpeed() const { return speed_; }

// 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 mp3s_; // the songs on the computer; could be a vector just as easily };

15.9 Exercise: Understanding the data structure

Walk through the MP3 program that follows for some sample input and draw a diagram of the resulting map 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(); } }