{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

17-Sets_and_Multisets

# 17-Sets_and_Multisets - Associative Containers Data...

This preview shows pages 1–4. Sign up to view the full content.

Data Retrieval Sequential Container – O(N) Binary Search Tree – O(logN) SLOW! How about constant time, O(1) ? HOW? Use associative containers (e.g. sets and maps that allow retrieving elements by key)! Associative Containers Associative Containers

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Definition: Set is an associative container representing a collections of unique objects. Example: {“apples”, “oranges”, “tomatoes”} Element uniqueness preservation: { “apples” , “oranges”, “tomatoes”} + {“apples”} = { “apples” , “oranges”, “tomatoes”} Because “apples” already was in the original set! Operations: Union {1, 2, 3} {4, 5} = {1, 2, 3, 4, 5} Intersection { 1 , 2, 3 } { 1 , 3 , 4, 5} = { 1 , 3 } Subset { 1 , 3 } { 1 , 2, 3 } is true Difference { 1 , 2, 3 } - { 1 , 3 } = {2} Sets Sets
#include <set> set<string> mySet; size() returns set size; begin() returns iterator to the first item in the set; insert(const keyType& item) inserts new element into the set; erase(const keyType& item) removes element from the set; iterator find(const keyType& item) finds element in the set (if the item is not found end() iterator is returned).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}