190 likes | 329 Views
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.
E N D
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 • 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
pair • The STL includes ‘utilities’ • Class pair treats two values as single unit • Members: first, second (both public) • Convenience function: make_pair
Pair example #include <string> … string s1("cat"); string s2("catnip"); pair<string, string> favorite(make_pair(s1, s2));
<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
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;
Algorithms • <algorithm> is an example of generic programming • Object-oriented binds methods + members -- generic applies algorithms to many data types • Examples: sort, swap
Sort • <algorithm> sort requires random access, < • Sort works on a range #include <algorithm> … sort(begin, end); sort(begin, end, op); stable_sort(begin, end);
And the result is .. string s(“tictoc”); sort( s.begin(), s.end() ); sort( &s[0], &s[2] );
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)
Associative containers • Sets • Multisets • Maps • Multimaps
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
Map “dog”“woof” “cat”“meow”
Multimap “joe”“850-1234” “joe”“joe@njit.edu”
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
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;
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
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();