Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:

Similar presentations


Presentation on theme: "The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:"— Presentation transcript:

1 The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this: template struct pair { T1 first; T2 second; pair(): first(T1()), second(T2()) {} pair(const T1& x, const T2& y):first(x), second(y) {} template pair(const pair & p) : first(p.first), second(p.second) {} }; pair proves convenient on its own, but it’s also used repeatedly in the standard associative containers.

2 pair pair p1;//Error pair p2; void f(pair ); void g(pair ); … pair p(42, “hello”); f(p); g(p);

3 maps and multimaps maps and multimaps hold (key, value) pairs. They literally hold pairs. map and multimap are containers of pair. –The key is const, because it’s used by the container’s comparison function to figure out where pair goes in the container. –Recall that set and multiset elements are also immutable. map m; // m contains objects of type // pair multimap mm; // mm contains objects of type // pair

4 maps and multimaps Hence, map and multimap iterators “point” to pair objects. –To get at the key or the value, you must refer to first or second. if (*mm.begin() == 10) … // error! can’t compare pair // and int if (mm.begin()->first == 10)... // fine

5 maps, multimaps, sets, and multisets Behaviorally, a map acts almost the same as a set. Search functions (e.g., find(), lower_bound(), upper_bound(), etc.) look only at key values. Insertions look only at key values when determining where pairs should go. Similarly, a multimap acts almost the same as a multiset.

6 map and multimap Functionality map and multimap support: Universal Standard Container Functionality User-specified comparison functions set’s/multiset’s search functions, –find(), count(), lower_bound(), upper_bound(), and equal_range(). –Remember that only keys are taken into account! set’s/multiset’s three insert() functions, –Single-element insertions require pair objects! map m; m.insert(pair (36, 10.57)); m.insert(make_pair(36, 10.57)); m.insert(m.end(), make_pair(40, 2.29)); Insertion of a range works the same as for set/multiset.

7 map and multimap Functionality set’s/multiset’s three erase() functions. Erasing all elements with a given value takes only a key! multimap mm; typedef multimap ::size_type ST; // erase all elements in mm with key // equivalent to "Dog" ST numErased = mm.erase("Dog"); Erasing one or more elements pointed to by iterators works the same as for set/multiset.

8 operator[]() for maps maps make excellent associative arrays, thanks to map::operator[](). map prices; prices["MSFT"] = 90.625; prices["DELL"] = 46.75; prices["MSFT"] = 89.5; prices["DELL"] = 49.1875; If the key doesn’t exist, it’s inserted (the corresponding value is default- initialized). –You can’t use map ::operator[]() unless V has a default constructor. If the key does exist, its value is updated.

9 operator[]() for maps Caveats: operator[]() makes it easy to insert the wrong key. prices["DULL"] = 49.1875; Insertion via operator[]() is less efficient than a direct call to insert(). –operator[]() default constructs the value, then makes an assignment. –A direct call to insert() simply copy constructs the value. prices["INAP"] = 92.5; prices.insert( make_pair(string("INAP"), 92.5)); There is no operator[]() for multimaps. –How would you know which element value to update?


Download ppt "The Utility Class pair All associative containers make use of the pair template, which is defined in. Slightly stripped down, it looks basically like this:"

Similar presentations


Ads by Google