270 likes | 411 Views
CSE 30331 Lecture 15 – Maps. STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6. Brief Intro to Maps. Maps are essentially sets of .. Key-value pairs Sorted and accessed by key Multimaps are essentially multisets of … Key-value pairs Sorted and accessed by key.
E N D
CSE 30331Lecture 15 – Maps • STL Map & Multimap • Ford & Topp • Chapter 11 • Josuttis • Sections: 6.5 & 6.6
Brief Intro to Maps • Maps are essentially sets of .. • Key-value pairs • Sorted and accessed by key • Multimaps are essentially multisets of … • Key-value pairs • Sorted and accessed by key
Key-Value Data A map stores data as a key-value pair. In a pair, the first component is the key; the second is the value. Each component may have a different data type.
2 4 1 5 3 Usually as a Balanced Binary (Search) Tree Map & Set Implementation
Map Ordering • The map is ordered using comparison operator less<T>, unless otherwise specified • Example: map<string,int,greater<string> > myMap; • The Comparison must provide a “strict weak ordering” with these properties • Antisymetric [if x<y then !(y<x)] • Transitive [if x<y and y<z then x<z] • Irreflexive [x<x is false] • Transitivity of equivalence • [if !(x<y) && !(y<x) && !(y<z) && !(z<y) then !(x<z) && !(z<x)] • Roughly the same as saying if x==y and y==z then x==z
Access & Search • Access is by iterator • Through the iterator we can change the value but not the key of a particular item • To change a key we must erase the item and insert a new one • Map has [ ] operator for associative array-like access • Set does NOT • Search is very efficient -- O(logN)
The pair structure • Defined in <utility> • Template struct with two fields (first & second) • Constructors and the like … • Prototype: pair<T1,T2>(T1& val1, T2& val2); • Example : pair<string,int> myPair(“JHS”,51); • Function make_pair() is used to construct a nameless pair for passing as parameter, etc • template<class T1, classT2> • make_pair(T1& val1,T2& val2); • Example: make_pair(“JHS”,51):
Using pairs • Insert function … • Takes a key/value pair as parameter • Returns an interator/bool pair as value • So we can use the returned pair to make decisions typedef map<string,int> siMap; typedef siMap::value_type kvPair; typedef pair<siMap::iterator, bool> ibPair; siMap myMap; ibPair p; string name = “JHS”; p = myMap.insert(kvPair(name,51)); if (p.second) cout << “ Inserted new pair”; else cout << “already had pair with key and value “ << p.first->first << “ and “ << p.first->second;
map(); Create an empty map. This is the Default Constructor. CLASS map CLASS map <map> <map> Constructors Operations map(T *first, T *last); Initialize the map using the range [first, last). bool empty() const; Is the map empty? int size() const; Return the number of elements in the map.
int count(const T& key) const; Search for key in the map and return 1 if it is in the map and 0 otherwise. CLASS map <map> Operations iterator find(const T& key); Search for key in the map and return an iterator pointing at it, or end() if it is not found. Const_iterator find(const T& key) const; Constant version.
CLASS map <map> Operations pair<iterator, bool> insert(const T& item); If key (item.first) is not in the map, insert and return a pair whose first element is an iterator pointing to the new element and whose second element is true. Otherwise, return a pair whose first element is an iterator pointing at the existing element and whose second element is false. Postcondition: The set size increases by 1 if key is not in the map. int erase(const T& key); If key is in the map, erase all items it is part of and return number of removals; otherwise, return 0. Postcondition: The set size decreases by number of removals if key is in the map.
void erase(iterator pos); Erase the item pointed to by pos. Preconditions: The map is not empty, and pos points to a valid map element. Postcondition: The set size decreases by 1. CLASS map <map> Operations void erase(iterator first, iterator last); Erase the elements in the range [first, last). Precondition: The map is not empty. Postcondition: The map size decreases by the number of elements in the range.
iterator begin(); Return an iterator pointing at the first member in the map. CLASS map <map> Operations const_iterator begin(const); Constant version of begin(). iterator end(); Return an iterator pointing just past the last member in the map. const_iterator end() const; Constant version of end().
Associative Arrays • Map has [ ] operator to give array-like access • M[key] • returns a reference to the value of the item with given key • If no item is in map with key, then a new item is inserted in the map with the specific key and a default value (from the default constructor for its type) • M[“bob”]=8.3 • Either changes value of exiting item to 8.3 • Or creates new item (“bob”,0.0) and then sets value to 8.3 in second stage
Example program • Concordance (prg11_5.cpp in Ford&Topp) • Uses a map of sets • Map<string,set<int> > concordMap; • Each entry corresponds to a word and the associated “value” is a set of line numbers indicating lines on which the word is found • Reads a text file character by character • While there is another word • Insert word/line_number pair into map • If on new line increment line counter
Concordance // File: prg11_5.cpp // prompt user for name of a text file containing identifiers, // where an identifier begins with a letter ('A'..'Z', 'a'..'z') // followed by 0 or more letters or digits ('0'..'9'). function // concordance() takes a file name as an argument and uses a map // with key string and value set<int> to determine each identifier, // the number of lines on which it occurs, and the line numbers on // which it appears. concordance() calls writeConcordance() to // display the results in the format // identifier n: line# line# line# . . . #include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> #include <string> #include <set> #include <map> #include <ctype.h> // for functions isalpha() and isdigit() #include "d_util.h“ // for writeContainer(iter1,iter2) using namespace std;
Concordance // input filename and output a concordance void concordance(const string& filename); // output the identifier, the number of lines containing the // identifier, and the list of lines having the identifier void writeConcordance(const map<string,set<int> >& concordance); int main() { string filename; // get the file name cout << "Enter the file name: "; cin >> filename; cout << endl; // create the concordance concordance(filename); return 0; }
Concordance void concordance(const string& filename) { // declare the concordance map map<string, set<int> > concordanceMap; // objects to create identifiers and maintain line numbers char ch; string identifier = ""; bool beginIdentifier = true; int lineNumber = 1; // file objects ifstream fin; // open the input file fin.open(filename.c_str()); if (!fin) { cerr << "Cannot open '" << filename << "'" << endl; exit(1); }
Concordance // read the file character by character to determine each // identifier and update line numbers while(true) { fin.get(ch); if (!fin) break; // check for a letter that begins an identifier if (isalpha(ch) && beginIdentifier) { // add char to identifier and continue scan identifier += ch; beginIdentifier = false; } // check if subsequent letter or digit in an identifier else if ((isalpha(ch) || isdigit(ch)) && !beginIdentifier) identifier += ch;
Concordance else // not part of an identifier { // if we have just finished with an identifier, use the index // operator to access a map entry with identifier as the key. if not // in the map, the operator adds an entry with an empty set as the // value component. if in the map, the operator accesses the set // component. in either case, insert the current line number if (!beginIdentifier && identifier != "") concordanceMap[identifier].insert(lineNumber); if (ch == '\n') lineNumber++; // increment lineNumber when ch == '\n' // reset objects preparing for next identifier beginIdentifier = true; identifier = ""; } } // output the concordance writeConcordance(concordanceMap); }
Concordance void writeConcordance(const map<string,set<int> >& concordance) { map<string, set<int> >::const_iterator iter = concordance.begin(); int i; while (iter != concordance.end()) { cout << (*iter).first; // output key // pad output to 12 characters using blanks if ((*iter).first.length() < 12) for (i=0;i < 12 - (*iter).first.length();i++) cout << ' '; // output number of lines and specific line numbers where id occurs cout << setw(4) << (*iter).second.size() << ": "; writeContainer((*iter).second.begin(),(*iter).second.end()); cout << endl; iter++; } cout << endl; }
Concordance File: "concord.txt" int m, n; double a = 3, b = 2, hypotenuse; cin << m; if (n <= 5) n = 2*m; else n = m * m; cout << n << endl; hypotenuse = sqrt(a*a + b*b); cout << hypotenuse << endl; Run: Enter the file name: concord.txt a 2: 2 12 b 2: 2 12 cin 1: 4 cout 2: 10 13 double 1: 2 else 1: 7 endl 2: 10 13 hypotenuse 3: 2 12 13 if 1: 5 int 1: 1 m 4: 1 4 6 8 n 5: 1 5 6 8 10 sqrt 1: 12
Next Assignment • Web Crawler (local) • Read set of connected (local) HTML pages • Build a vector of filenames • Build a map of <string, set <int> > pairs • Each string is a map key and is a word from a page • Each set contains the id’s of all web pages containing the word • The web page id is the position of its name in the vector • Case is not meaningful (word = WORD = WoRd) • Each page should be processed only once • All tags < … > are ignored, except for those leading to other pages • Format of final output IS important
Summary • Sets and maps are associative containers • Both store and retrieve data by value rather by position. • A set is a collection of keys, where each key is unique. • In a multiset each key may appear multiple times. • A map is a collection of key-value pairs, where each key is unique. • In a multimap, each key may be part of multiple key-value pairs.
Summary • Implementation • Binary search tree ideal, since it is an ordered associative data structure and its iterators traverse its values in order. • Map • Often called an associative array because applying the index operator with the key as its argument accesses the associated value.
Summary • Multiset • Like a set, except a key can occur more than once. • The member function count() and equal_range() deal with duplicate values. • Multimap • Like a map, except a key can occur more than once, each time with a (potentially) different value. • The member function count() and equal_range() deal with duplicate values.