200 likes | 215 Views
Learn about associative containers in C++ STL, including sets, maps, generic containers, and the Pair template. Discover the functionalities, insertion operations, iterators, and examples.
E N D
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
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
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
The Pair Template Some background
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); };
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) { }
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);
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); };
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) {}
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
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
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
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; }
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
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
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()
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