1 / 37

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida. Maps and Multimaps. 3. 3. 1. 2. 6. 4. 1. 1. 5. 3. 2. 3. y. x. x. y. z. z. z. y. y. z. y. y. Objectives. Map and Multimap. Description.

baba
Télécharger la présentation

CIS 4930 Application Development Using C++ Dr. Kun Suk Kim CISE Department, University of Florida

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. CIS 4930Application Development Using C++Dr. Kun Suk KimCISE Department, University of Florida Maps and Multimaps

  2. 3 3 1 2 6 4 1 1 5 3 2 3 y x x y z z z y y z y y Objectives • Map and Multimap

  3. Description • Map is a Sorted Associative Container • Associates objects of type Key with objects of type Data • Map is a Pair Associative Container • Its value type is pair<const Key, Data> • Map is a Unique Associative Container • No two elements have the same key • Whereas multimap allows duplicates

  4. Description • Map has the important property • Inserting a new element into a map does not invalidate iterators that point to existing elements • Erasing an element from a map also does not invalidate any iterators • Except for iterators that actually point to the element that is being erased

  5. Definition • Defined in the standard header map • Used as #include <map> namespace std { template <class Key, class Data, class Compare = less<Key>, class Alloc = allocator<pair<const Key, T> > > class map; template <class Key, class Data, class Compare = less<Key>, class Alloc = allocator<pair<const Key, T> > > class multimap; }

  6. Template Parameters • Key • Map's key type • This is also defined as map::key_type  • Data • Map's data type • This is also defined as map::data_type 

  7. Template Parameters • Compare • Key comparison function, a Strict Weak Ordering whose argument type is key_type • It returns true if its first argument is less than its second argument, and false otherwise • This is also defined as map::key_compare • Default: less<Key> • Alloc • Map's allocator • Used for all internal memory management

  8. 3 4 2 9 12 6 1 5 8 11 10 7 q z w y y x y z q y x y Internal Structure of Maps and Multimaps • Balanced binary tree • Elements are key/value pairs

  9. Abilities of Maps and Multimaps • Sort their elements automatically according to the element’s keys • Good performance when searching for elements that have a certain key • Bad performance when searching for elements that have a certain value • May not change the key of an element directly • This might compromise the correct order • A direct modification of the value of the element is possible (provided the type of the value is not constant)

  10. Constructors • map() • Creates an empty map • map(const map&) • The copy constructor • map(const key_compare& comp) • Creates an empty map, using comp as the key_compare object

  11. Constructors • template <class InputIterator> map(InputIterator f, InputIterator l) • Creates a map with a copy of a range • template <class InputIterator> map(InputIterator f, InputIterator l, const key_compare& comp) • Creates a map with a copy of a range, using comp as the key_compare object

  12. Non-Modifying Operations • size_type size() const • Returns the size of the map • size_type max_size() const • Returns the largest possible size of the map • bool empty() const • True if the map's size is 0

  13. Comparisons • bool operator==(const map&, const map&) • Tests two maps for equality • This is a global function, not a member function • bool operator<(const map&, const map&) • Lexicographical comparison • This is a global function, not a member function

  14. Comparisons • Comparisons are provided only for containers of the same type • The key, value, and sorting criterion must be the same type • Otherwise, a type error occurs at compile time • Example: std::map<float,std::string> c1; // sorting criterion: less std::map<float,std::string,std::greater<float> > c2; if (c1 == c2) // ERROR: different types

  15. Using Maps as Associative Arrays • data_type& operator[](const key_type& k) • Returns a reference to the object that is associated with a particular key • If the map does not already contain such an object, operator[] inserts the default object data_type()

  16. Using Maps as Associative Arrays • Advantage: a more convenient interface • std::map<std::string,float> coll; coll[“otto”] = 7.7; • First it inserts “otto”/float(), then it assigns 7.7 • Is slower than the usual way of inserting elements for maps • Disadvantage: might insert new elements by accident or mistake • std::cout << coll[“ottto”]; • It inserts a new element with key “ottto” and prints its value, which is 0 by default • It should have generated an error message telling you that you wrote “otto” incorrectly

  17. Iterator Functions and Element Access • iterator begin() • const_iterator begin() const • Returns an iterator (const_iterator) pointing to the beginning of the map • iterator end() • const_iterator end() const • Returns an iterator (const_iterator) pointing to the end of the map

  18. Iterator Functions and Element Access • reverse_iterator rbegin() • const_reverse_iterator rbegin() const • Returns a reverse_iterator (const_reverse_iterator) pointing to the beginning of the reversed map • reverse_iterator rend() • const_reverse_iterator rend() const • Returns a reverse_iterator (const_reverse_iterator) pointing to the end of the reversed map

  19. Example of the Use of Iterators • std::map<std::string,float> coll; … std::map<std::string,float>::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) std::cout << “key: “ << pos->first << “ ” << “value: “ << pos->second << std:endl; • pos->first = “hello”; // ERROR at compile time pos->second = 13.5; // OK

  20. Changing the Key of an Element std::map<std::string,float> coll; … // insert new element with value of old element coll[“new_key”] = coll[“old_key]; // remove old element coll.erase(“old_key”);

  21. Example for (pos = stocks.begin(); pos != stocks.end(); ++pos) cout << "stock: " << pos->first << "\t“ << "price: " << pos->second << endl; cout << endl; #include <iostream> #include <map> #include <string> using namespace std; int main(){ typedef map<string,float> StringFloatMap; StringFloatMap stocks; stocks["BASF"] = 369.50; stocks["VW"] = 413.50; stocks["Daimler"] = 819.00; stocks["BMW"] = 834.00; stocks["Siemens"] = 842.20; StringFloatMap::iterator pos; stock: BASF price: 369.5 stock: BMW price: 834 stock: Daimler price: 819 stock: Siemens price: 842.2 stock: VW price: 413.5

  22. Example for (pos = stocks.begin(); pos != stocks.end(); ++pos) pos->second *= 2; for (pos = stocks.begin(); pos != stocks.end(); ++pos) cout << "stock: " << pos->first << "\t“ << "price: " << pos->second << endl; cout << endl; stocks["Volkswagen"] = stocks["VW"]; stocks.erase("VW"); for (pos = stocks.begin(); pos != stocks.end(); ++pos) cout << "stock: " << pos->first << "\t“ << "price: " << pos->second << endl; }

  23. Example stock: BASF price: 369.5 stock: BMW price: 834 stock: Daimler price: 819 stock: Siemens price: 842.2 stock: VW price: 413.5 stock: BASF price: 739 stock: BMW price: 1668 stock: Daimler price: 1638 stock: Siemens price: 1684.4 stock: VW price: 827 stock: BASF price: 739 stock: BMW price: 1668 stock: Daimler price: 1638 stock: Siemens price: 1684.4 stock: Volkswagen price: 827

  24. Assignments • map& operator=(const map&) • The assignment operator • void swap(map&) • Swaps the contents of two maps

  25. Inserting and Removing Elements • pair<iterator, bool> insert(const value_type& x) • Inserts x into the map • iterator insert(iterator pos, const value_type& x) • Inserts x into the map, using pos as a hint to where it will be inserted • template <class InputIterator> void insert(InputIterator, InputIterator) • Inserts a range into the map

  26. Inserting and Removing Elements • void erase(iterator pos) • Erases the element pointed to by pos • size_type erase(const key_type& k) • Erases the element whose key is k • Return the number of removed elements • void erase(iterator first, iterator last) • Erases all elements in a range • void clear() • Erases all of the elements

  27. Passing a Value into a Map • Use value_type • Provide correct type • std::map<std::string,float> coll; coll.insert(std::map<std::string,float>::value_type(“otto”, 22.3)); • Use pair<> • std::map<std::string,float> coll; // use implicit conversion coll.insert(std::pair<std::string,float>(“otto”, 22.3)); // use no implicit conversion coll.insert(std::pair<const std::string,float>(“otto”, 22.3));

  28. Passing a Value into a Map • Use make_pair() • std::map<std::string,float> coll; coll.insert(std::make_pair(“otto”, 22.3));

  29. Example of the Insertion std::map<std::string,float> coll; … if (coll.insert(std::make_pair(“otto”, 22.3)).second) std::cout << “OK, could insert otto/22.3” << std::endl; else std::cout << “Oops, could not insert otto/22.3” << std::endl;

  30. Example of Removing Only the First Element typedef std::multimap<std::string,float> StringFloatMMap; StringFloatMMap coll; … // remove first element with passed key StringFloatMMap::iterator pos; pos = coll.find(key); if (pos != coll.end()) coll.erase(pos);

  31. Common Error in Remove Operations • typedef std::map<std::string,float> StringFloatMap; StringFloatMap coll; StringFloatMap::iterator pos; … for (pos = coll.begin(); pos != coll.end(); ++pos) if (pos->second == value) coll.erase(pos); // RUNTIME ERROR • Calling erase() invalidates pos as an iterator of coll • Calling ++pos results in undefined behavior

  32. Correct Way • typedef std::map<std::string,float> StringFloatMap; StringFloatMap coll; StringFloatMap::iterator pos; … // remove all elements having a certain value for (pos = coll.begin(); pos != coll.end(); ) if (pos->second == value) coll.erase(pos++); else ++p; • pos++ increments pos so that it refers to the next element but yields a copy of its original value • pos doesn’t refer to the element that is removed when erase() is called

  33. Special Search Operations • size_type count(const key_type& k) • Counts the number of elements whose key is k • iterator lower_bound(const key_type& k) • const_iterator lower_bound(const key_type& k) const • Finds the first element whose key is not less than k • iterator upper_bound(const key_type& k) • const_iterator upper_bound(const key_type& k) const • Finds the first element whose key greater than k

  34. Special Search Operations • pair<iterator, iterator> equal_range(const key_type& k) • pair<const_iterator, const_iterator> equal_range(const key_type& k) const • Finds a range containing all elements whose key is k • iterator find(const key_type& k) • const_iterator find(const key_type& k) const • Finds an element whose key is k

  35. Example #include <iostream> #include <map> #include <string> #include <iomanip> using namespace std; int main(){ typedef multimap<string,string> StrStrMMap; StrStrMMap dict; dict.insert(make_pair(string("day"),string("Tag"))); dict.insert(make_pair(string("strange"),string("fremd"))); dict.insert(make_pair(string("car"),string("Auto"))); dict.insert(make_pair(string("smart"),string("elegant"))); dict.insert(make_pair(string("trait"),string("Merkmal"))); dict.insert(make_pair(string("strange"),string("seltsam"))); dict.insert(make_pair(string("smart"),string("raffiniert")));

  36. Example dict.insert(make_pair(string("smart"),string("klug"))); dict.insert(make_pair(string("clever"),string("raffiniert"))); StrStrMMap::iterator pos; cout.setf (ios::left, ios::adjustfield); cout << ' ' << setw(10) << "english " << "german " << endl; cout << setfill('-') << setw(20) << "" << setfill(' ') << endl; for (pos = dict.begin(); pos != dict.end(); ++pos) cout << ' ' << setw(10) << pos->first << pos->second << endl; cout << endl; string word("smart"); cout << word << ": " << endl; for (pos = dict.lower_bound(word); pos != dict.upper_bound(word); ++pos) cout << " " << pos->second << endl; word = ("raffiniert");

  37. Example cout << word << ": " << endl; for (pos = dict.begin(); pos != dict.end(); ++pos) if (pos->second == word) cout << " " << pos->first << endl; return 0; } strange seltsam trait Merkmal smart: elegant raffiniert klug raffiniert: clever smart english german -------------------- car Auto clever raffiniert day Tag smart elegant smart raffiniert smart klug strange fremd

More Related