1 / 19

Multimaps

Multimaps. 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. Resources -- texts.

emlyn
Télécharger la présentation

Multimaps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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> … string s1("cat"); string s2("catnip"); pair<string, string> favorite(make_pair(s1, s2));

  6. <fstream> • Descendent class of ios and iostream • Permits reading and writing files as streams, using << and >> #include <fstream> … 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 <string> #include <vector> #include <list> using namespace std; … Vector< list<string> > vls;

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

  9. Sort • <algorithm> sort requires random access, < • Sort works on a range #include <algorithm> … 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 1st inst of key, or end() lower_bound(k); // iter to 1st 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();

More Related