1 stl map & multimap ford & topp chapter 11 josuttis sections: 6.5 & 6.6 cse 30331...

27
1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

Upload: yosef-neighbours

Post on 22-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

1

STL Map & Multimap

Ford & Topp Chapter 11

Josuttis Sections: 6.5 & 6.6

CSE 30331Lecture 15 – Maps

Page 2: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

2

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

Page 3: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

3

Key-Value Data

k ey v alu e

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.

Page 4: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

4

Maps

Key - v a lu e P a ir

M ap as a Set o f P airs

k ey v alu e

k ey v alu e

k ey v alu e

k ey v alu e

k ey v alu e

Page 5: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

5

Map & Set Implementation

2

4

53

1

Usually as a Balanced Binary (Search) Tree

Page 6: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

6

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

Page 7: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

7

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)

Page 8: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

8

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):

Page 9: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

9

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;

Page 10: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

10

CLASS map Constructors <map>

map();Create an empty map. This is the Default Constructor.

map(T *first, T *last);Initialize the map using the range [first, last).

CLASS map Operations <map>

bool empty() const;Is the map empty?

int size() const;Return the number of elements in the map.

Page 11: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

11

CLASS map Operations <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.

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.

Page 12: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

12

CLASS map Operations <map>

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.

Page 13: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

13

CLASS map Operations <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.

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.

Page 14: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

14

CLASS map Operations <map>

iterator begin();Return an iterator pointing at the first member in the

map.

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().

Page 15: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

15

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

Page 16: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

16

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

Page 17: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

17

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;

Page 18: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

18

Concordance // input filename and output a concordancevoid concordance(const string& filename);

// output the identifier, the number of lines containing the// identifier, and the list of lines having the identifiervoid 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;}

Page 19: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

19

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); }

Page 20: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

20

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;

Page 21: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

21

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);}

Page 22: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

22

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;}

Page 23: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

23

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 12b 2: 2 12cin 1: 4cout 2: 10 13double 1: 2else 1: 7endl 2: 10 13hypotenuse 3: 2 12 13if 1: 5int 1: 1m 4: 1 4 6 8n 5: 1 5 6 8 10sqrt 1: 12

Page 24: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

24

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

Page 25: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

25

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.

Page 26: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

26

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.

Page 27: 1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE 30331 Lecture 15 – Maps

27

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.