stl associative containers

25
CSCI 3110

Upload: leigh-cote

Post on 01-Jan-2016

67 views

Category:

Documents


0 download

DESCRIPTION

STL Associative Containers. CSCI 3110. STL Containers. Sequence Containers – store sequences of values vector deque list Associative Containers – use “keys” to access data rather than position (Account #, ID, SSN, …) set multiset map multimap. Containers continued. - PowerPoint PPT Presentation

TRANSCRIPT

CSCI 3110

Sequence Containers – store sequences of values◦ vector◦ deque◦ list

Associative Containers – use “keys” to access data rather than position (Account #, ID, SSN, …)◦ set◦ multiset◦ map◦ multimap

Container Adapters – specialized interfaces to general containers◦ stack◦ queue◦ priority_queue

Stores elements based on a key Key can consist of one or more attributes to

uniquely identify each element (we will assume only one attribute).

Example: Department of Motor Vehicles (DMV) uses license-plate # to identify a vehicle.

Similar to vector & list – it is another storage structure with operations to access & modify elements.

Main difference is that associative-container uses the key rather than an index (vector) or linear search (list) to retrieve an element.

Stores a set of values (i.e., “keys”) Values are unique (stored only once) Implemented as a balanced binary search tree

(red-black tree)◦ #include <set>◦ set<string> s;

Fast insert and delete◦ insert, erase

Fast search◦ find

Other operations◦ size, empty, clear, . . .

“if”

“operator”

“while” “class”

“template”

Stores a set of values (i.e., “keys”) Like set, but values need not be unique Implemented as a balanced binary search tree

(red-black tree)◦ #include <set>◦ multiset<string> ms;

Fast insert and delete◦ insert, erase

Fast search◦ find

Other operations◦ size, empty, clear, . . .

Stores a set of (key, value) pairs Each key has one value Implemented as a balanced binary search tree

(red-black tree)#include <map>//define a map with//keys of type string//and values of intmap<string, int> m;

Fast insert and deletem[“fred”] = 99;insert, erase

“fred” | 99

“sue” | 86

“james” | 52

Fast search◦ int x = m[“fred”];◦ find

Other operations◦ size, empty, clear, . . .

STL associative containers are implemented internally using a red-black tree which is a BST◦ Key classes stored in associative containers must

implementbool operator< (T other)

◦ If they don’t, you can alternately pass a comparator class to the template that it should use to order elements

◦ A comparator class overridesbool operator() (T a, T b);

set<Employee *> employees; //BST sorts based on pointers //(probably not what you want)

class EmployeeComparator{public:

bool operator() (const Employee* a, const Employee* b)

{ return (a->getID() < b->getID());}

};//create a set that sorts based on employee IDsset<Employee*, EmployeeComparator> employees;

Classes that will be stored in STL containers should explicitly define the following:◦ Default constructor◦ Copy constructor◦ Destructor◦ operator =◦ operator==◦ operator<

Not all of these are always necessary, but it might be easier to define them than to figure out which ones you actually need

Many STL programming errors can be traced to omitting or improperly defining these methods

Copy constructor: Copy constructor:map<char,int> m;map<char,int> m2(m);

An STL map is implemented as a tree-structure, where each node holds a “pair”

Most important to know when retrieving data from the table◦ Some functions return the pair, not just the value

A pair has two fields, first (holding the key) and second (holding the value)

If you have a pair object, you can use the following code to print the key and value:cout << myPairObject.first << “ “ << myPairObject.second;

If you have a pointer to the pair object, use the arrow operator insteadcout << myPairObject->first << “ “ << myPairObject->second;

Tree structure ◦ logarithmic time inserts, finds, deletes

void clear()

remove all elements

bool empty()

returns true if empty, false otherwise

size_type max_size()

returns max number of elements map can hold (usually an integer returned) [capacity]

size_type size()

return the number of elements currently in the map (usually an integer returned) [actual size]

iterator begin()

returns an iterator to the first element in the map (the first when sorted, due to storage mechanism)

iterator end()

returns an iterator to the last element in the map (the last when sorted)

reverse_iterator rbegin()

returns a reverse iterator to the end of the mapreverse_iterator rend()

returns a reverse iterator to the start of the map

pair<iterator,bool> insert(const value_type &val)

Insert val into the map, if it’s not already there. Return pair<iterator,true> if successful, pair<iterator,false> if fails.

iterator insert(iterator I, const value_type &val)

Insert val into the map, after the value specified by i. Iterator to inserted element is returned.

template <class InIter> void insert(InIter start, InIter end)

Insert a range of elements

void erase (iterator i)

Remove the element pointed to by i.

size_type erase(const key_type & k)

Remove from the map elements that have keys with the value k.

void erase(iterator start, iterator end)

Remove the elements in the range start to end

iterator find(const key_type &k)

Returns an iterator to the specified key. If the key is not found, an iterator to the end of the map is returned.

size_type count(const key_type &k) const

Returns the number of times a key k occurs in the map (0 or 1)

reference operator[](const key_type &k)Returns a reference to the value associated with the key k. If the key is not found in the map, the key and a default constructed instance of the value type is inserted in the amp.

iterator lower_bound(const key_type &k)

Returns an iterator to the first element in the map with a key >= k

iterator upper_bound(const key_type &k) const

Returns an iterator to the first element in the map with a key strictly > k

pair<iterator, iterator> equal_range(const key_type &k)

Returns a pair of iterators that point to the upper bound and the lower bound in the map for the specified key

Map between characters and ASCII representations

Map betweenCharacters andASCII representations

Same program, exploiting []

Print all entries in map in forward and reverse order

Storing objects, requiring an overload of the < operator for the key type