Multimaps
Download
1 / 19

Multimaps - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Multimaps' - emlyn


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Resources web
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
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
Pair example

#include <string>

string s1("cat");

string s2("catnip");

pair<string, string> favorite(make_pair(s1, s2));


Fstream
<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
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
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
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
Associative containers

  • Sets

  • Multisets

  • Maps

  • Multimaps


Map v multimap
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
Multimap

“joe”“850-1234”

“joe”“[email protected]


Multimap constructors
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
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
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
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();


ad