Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.

Similar presentations


Presentation on theme: "Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources."— Presentation transcript:

1 Multimaps

2 Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources http://www.sgi.com/tech/stl/ http://www.cs.rpi.edu/projects/STL/htdocs/stl.html

3 Resources -- texts Josuttis, The C++ Standard Library Austern, Generic Programming and the STL Musser, STL Tutorial and Reference Guide Schildt, STL Programming Skansholm, C++ from the Beginning

4 pair The STL includes ‘utilities’ Class pair treats two values as single unit Members: first, second (both public) Convenience function: make_pair

5 Pair example #include … string s1("cat"); string s2("catnip"); pair favorite(make_pair(s1, s2));

6 Descendent class of ios and iostream Permits reading and writing files as streams, using > #include … ifstream inFile(“fileName”); // read ofstream inFile(“fileName”); // write fstream inFile(“fileName”);// r/w

7 Nested containers A container can hold any data type A container can hold another container Example: #include using namespace std; … Vector > vls;

8 Algorithms is an example of generic programming Object-oriented binds methods + members -- generic applies algorithms to many data types Examples: sort, swap

9 Sort sort requires random access, < Sort works on a range #include … sort(begin, end); sort(begin, end, op); stable_sort(begin, end);

10 And the result is.. string s(“tictoc”); sort( s.begin(), s.end() ); sort( &s[0], &s[2] );

11 Swap Swap acts by manipulating pointers Swap may replace a = b; if b is no longer needed Swap is more efficient than assignment (constant regardless of size of data structure)

12 Associative containers Sets Multisets Maps Multimaps

13 Map v multimap Both store {key, value} pairs In both, key and value may be of any type In both, values may be duplicated In map, key must be unique In multimap, you may have duplicate keys

14 Map “dog” “woof” “cat” “meow”

15 Multimap “joe” “850-1234” “joe” “joe@njit.edu”

16 Multimap constructors map c;// empty map map c(op);// empty map w/ using op to sort map c1(c2);// copy constructor map c(beg, end);// initialize with beg-end range map c(beg, end, op);// init and specify op

17 Multimap comparisons c.size();// number of elements c.empty();// short for size == 0 c1 == c2;// these boolean operators do what c1 != c2;// you’d expect c1 < c2; c1 > c2; c1 >= c2; c1 <= c2;

18 Key ranges count(key);// # elems w/ this key find(key);// iter to 1 st inst of key, or end() lower_bound(k);// iter to 1 st key >= k upper_bound(k);// iter to last key >= k equal_range(k);// iters to first and last // key == k

19 Insertion/deletion c.insert(pair);// return pos of inserted elem Easiest way to insert is to use make_pair: m.insert(make_pair(key, val)); m.erase(pos); m.erase(begin, end); m.clear();


Download ppt "Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources."

Similar presentations


Ads by Google