week 5 - associative containers: sets and maps. 2 2 main index main index content s content s...

28
Week 5 - Associative Containers: sets and maps

Upload: sydney-hopkins

Post on 18-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Week 5- Associative Containers:

sets and maps

Page 2: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

22 Main IndexMain Index

ContentsContents

Container TypesSequence Containers

Adapter Containers

Associative Containers

Vector Stack Set, Multiset

Deque Queue Map, Mutltimap

List Priority Queue

Page 3: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Associative containers

• Designed to be especially efficient in accessing its elements by their key, as opposed to sequence containers which are more efficient in accessing elements by their position.

• A programmer can use the key without concern for how elements are physically stored and how operations make the association between the key and an element.

• Insertion, deletion, and testing whether an element is in it, in logarithmic time - O(log n). Implemented using binary search trees.

Page 4: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Sets

• In a set, the data value is just the key:

Page 5: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

C++ Set

• Sets are containers that store unique elements following a specific order.

• The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

• Multisets are containers where multiple elements can have equivalent values.

Page 6: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Iterators

• Set containers keep their elements in ascending order, which follows the container's sorting criterion (By default, this is a less operator<).

• Hence, begin() points at the smallest element in the set, and end() points just past the largest element in the set.

Page 7: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

77 Main IndexMain Index ContentsContents

CLASS set Operations <set>

iterator begin();Return an iterator pointing at the first member in

the set.

iterator end();Return an iterator pointing just past the last

member in the set.

Page 8: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

88 Main IndexMain Index ContentsContents

CLASS set Constructors <set>

set();Create an empty set. This is the Default

Constructor.set(T *first, T *last);

Initialize the set by using the address range [first, last).CLASS set Operations <set>

bool empty() const;Is the set empty?

int size() const;Return the number of elements in the set.

Page 9: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

99 Main IndexMain Index ContentsContents

CLASS set Operations <set>

int count(const T& key) const;Search for key in the set and return 1 if it is in the

set and 0 otherwise.

iterator find(const T& key);Search for key in the set and return an iterator

pointing at it, or end() if it is not found.

Page 10: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

1010 Main IndexMain Index ContentsContents

CLASS set Operations <set>

pair<iterator, bool> insert(const T& key);If key is not in the set, insert it and then return a

pair whose first element is an iterator pointing to the new element and whose second element is true. Otherwise, return a pair whose first element is an iterator pointing at the existing element and whose second element is false.

Postcondition: The set size increases by 1 if key is

not in the set.int erase(const T& key);

If key is in the set, erase it and return 1; otherwise, return 0.

Postcondition: The set size decreases by 1 if key is

in the set.

Page 11: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

1111 Main IndexMain Index ContentsContents

CLASS set Operations <set>

void erase(iterator pos);Erase the item pointed to by pos.

Preconditions: The set is not empty, and pos points

to a valid set element.Postcondition: The set size decreases by 1.

void erase(iterator first, iterator last);Erase the elements in the range [first, last).

Precondition: The set is not empty.Postcondition: The set size decreases by the

number of elements in the range.

Page 12: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative
Page 13: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Set Operations

Page 14: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Set Operations

Page 15: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Set Operations

Page 16: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Example

Page 17: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Implementing Set Intersection

Complexity: Up to linear O(N)at most 2*(count1+count2)-1 comparisons(where countX is the distance between firstX and lastX)

C++ implementation

Page 18: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Maps

Page 19: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

C++ Map• Maps are associative containers that store elements formed by

a combination of a key value and a mapped value, following a specific order.

• The key values are generally used to sort and uniquely identify the elements, while the mapped values store the content associated to this key.

• The types of key and mapped value may differ, and are grouped together in a pair type.

• Multimaps are associative containers where multiple elements can have equivalent keys.

Page 20: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Maps• A map stores an element as a <key-value> pair,

The index operator [] leads us to refer to a map as an associative array. The index (key) for a map is not limited to integer values, but can be of any type.

Page 21: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

If k does not match the key of any element in the container, the function inserts a new element.

Page 22: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative
Page 23: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Map iterators

Page 24: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative
Page 25: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

insert()

Page 26: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

find() and erase()

Page 27: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

27

Binary search trees

sets, maps

The storage structure should be selected so thatthe container operations can be implementedefficiently

Page 28: Week 5 - Associative Containers: sets and maps. 2 2 Main Index Main Index Content s Content s Container Types Sequence Containers Adapter Containers Associative

Reading & HW reminder

• Book chapter 4.8

28

HW3 is due this Friday. (Submission before Thursday will receive 20

additional points)