1 / 20

Associative Containers Sets Maps Section 4.8

Learn about associative containers in C++ STL, including sets, maps, generic containers, and the Pair template. Discover the functionalities, insertion operations, iterators, and examples.

kburge
Download Presentation

Associative Containers Sets Maps Section 4.8

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. Associative Containers Sets MapsSection 4.8

  2. Associative Containers

  3. Generic Associative Containers • A container that is organized and accessible by value • Client may insert / remove any T-object in C<T> • Client may not determine position in C<T> • Bidirectional Iterators • Both ++ and -- operations are supported

  4. Multimodal / Unimodal Assoc. Containers • Multimodal associative container • Duplicate elements are allowed • Insert operations always increase size by 1 • Unimodal associative container • Duplicate elements not allowed • Insert operations have dual personality • If t is not in C, then Insert(t) • If t is in C, then overwrite the existing t

  5. Generic Sorted Associative Containers • A generic associative container • Traverse elements in sorted order for (C::Iterator I = c.Begin(); I != c.End(); ++I){ cout << *I; } • May be either unimodal or multimodal

  6. The Pair Template Some background

  7. The Pair Template • A class that holds a pair of items template <typename T> class Pair { public: T first; T second; Pair(); Pair(T t1, T t2); };

  8. The Pair Template (contd.) • Constructors template <typename T> Pair<T>::Pair() { } template <typename T> Pair<T>::Pair(T t1, T t2) : first(t1), second(t2) { }

  9. The Pair Template (contd.) • Declarations Pair<int> intPair1(1, 2); Pair<int> intPair2(3, 4); Pair<float> floatPair(1.1, 2.2); Pair< Pair<int> > anotherPair(intPair1, intPair2);

  10. The Pair Template (contd.) • A class that holds a pair of items with different types template <typename T1, typename T2> class Pair { public: T1 first; T2 second; Pair(); Pair(T1 t1, T2 t2); };

  11. The Pair Template (contd.) • Constructors template <typename T1, typename T2> Pair<T1, T2>::Pair() { } template <typename T1, typename T2> Pair<T1, T2>::Pair(T1 t1, T2 t2) : first(t1), second(t2) {}

  12. Sets&Maps

  13. Sets • Associative containers • Set • A sorted associative container that does not allow duplicates • Stores objects • Unimodal: duplicate objects not allowed • MultiSet • A sorted associative container that allows duplicates • Stores objects • Multimodal: duplicate objects OK • Also known as Bags

  14. The STL Set Template • set() // Creates an empty set. • set(const key_compare& comp) // Creates an empty set, use comp // for key comparison • set(iterator f, iterator l) // Creates a set with a copy of a range • set(iterator f, iterator l, const key_compare& comp) // Creates a set with a copy of a range, // using comp as the key_compare object • pair<iterator, bool> insert(const value_type& x) • iterator insert(iterator pos, const value_type& x) // Inserts x into the set, using pos as a hint to where it will be inserted • void insert(iterator I1, iterator I2) // Inserts a range into the set

  15. 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. • void erase(iterator first, iterator last) // Erases all elements in a range. • iterator find(const key_type& k) const // Finds an element whose key is k. • Logarithmic complexity for insertion, remove, search

  16. Example Set Clients • Inventory struct StockItem { // barcode, name, amount }; void print_inventory(std::ostream&os, const set<StockItem>& inventory) { set<StockItem>::iterator I; for (I = inventory.begin(); I != inventory.end(); ++I) { os << *I; } } • Customer accounts class Customer { // ssn, account_number, last_name, first_name… }; int main() { set<Customer> customers; }

  17. Maps • Associative container that associates objects of type Key with objects of type Data • Map • Stores (key, object) pairs • Unimodal on keys: duplicate keys not allowed • AKA: table, associative array • MultiMap • Stores (key, object) pairs • Multimodal: duplicate keys OK

  18. The STL Map Template • map() • map(const key_compare& comp) • map(iterator f, iterator l) • map(iterator f, iterator l, const key_compare& comp) • 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 hint to where it will be inserted • void insert(iterator, iterator) // Inserts a range into the map

  19. STL Map (continued) • 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. • void erase(iterator first, iterator last) // Erases all elements in a range. • iterator find(const key_type& k) // Finds an element whose key is k. • 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()

  20. struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; int main() { map<const char*, int, ltstr> months; months["january"] = 31; months["february"] = 28; months["march"] = 31; months["april"] = 30; months["may"] = 31; months["june"] = 30; months["july"] = 31; months["august"] = 31; months["september"] = 30; months["october"] = 31; months["november"] = 30; months["december"] = 31; cout << "june -> " << months["june"] << endl; map<const char*, int, ltstr>::iterator cur = months.find("june"); map<const char*, int, ltstr>::iterator prev = cur; map<const char*, int, ltstr>::iterator next = cur; ++next; --prev; cout << "Previous (in alphabetical order) is " << (*prev).first << endl; cout << "Next (in alphabetical order) is " << (*next).first << endl; } Map Usage Example

More Related