C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets...

15
C++ • Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually, the sort implementation is a binary tree which implies that elements cannot be changed directly. Thus, an element has to be deleted, modified, and then re-inserted. Made up of a key or search criteria. Use the insert() method in assigning elements, and erase() method for deletion. Also, emplace() will copy an element with the arguments passed in and return the position. Associative container based upon sorting of the element values. Ability to have a default sorting operation when declared. Use the #include <set> directive to pick up set support.

Transcript of C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets...

Page 1: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• Sets and MultisetsSet containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually, the sort implementation is a binary tree which implies that elements cannot be changed directly. Thus, an element has to be deleted, modified, and then re-inserted.– Made up of a key or search criteria.– Use the insert() method in assigning elements, and erase() method for

deletion. Also, emplace() will copy an element with the arguments passed in and return the position.

– Associative container based upon sorting of the element values.– Ability to have a default sorting operation when declared.– Use the #include <set> directive to pick up set support.

Page 2: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• Maps and MultimapsWhere sets sort elements based upon the association of the value of the element itself, maps and multimaps sort elements based upon some key in the element associated with the value of the element. Multimaps allow duplicate key/value pairs where as maps do not.– The key and value of a map or multimap must be copyable/movable.– The key must be comparable based upon the sorting criteria.– Usually binary tree based sorting.– Like sets, the key cannot be changed directly due to the side effect of

compromising the order.– The element must be removed, modified, and then re-inserted as is the case

with a set.

Page 3: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

Sets Map

Tem

plat

e i/

f

f(n)

HiddenHidden

Page 4: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• Unordered Sets and MapsUnordered containers are primarily considered hash tables where elements that are represented as part of the container are placed into the container in some arbitrary order. This is to say the container is a bucket that that elements are dropped into and then retrieved in some random fashion. With regular maps and sets, there is no sorting criteria that is followed by the user in order to place an element into a specific position – it is hidden from the user of the class.– Must use the #include directive with <unordered_set> and/or

<unordered_map> with possibly the <functional> include needed.– Hash tables use a hash code associated with a linked list of some sort. – There must be at least a forward iterator.– Rehashing is possible either through a complete rearrangement of data or

some incremental rearrangement.

Page 5: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++Unordered Container

(Hash Table)

Hashf(n)

Hidden

Tem

plat

e i/

f

Page 6: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++– No direct access operations provided and indirect access with iterators makes

key values constant.– As a developer, the things that can be defined are: the number of buckets, user

defined hash function, equivalence function, and load factor. Also, outside of the load factor, a user can force a rehash.

See “When to Use Which Container”, Section 7.12, pgs. 392 – 395, The C++ Standard Library (Josuttis).

Page 7: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• Function Objects and Lambdas– Function Object: functor (f(n)) has a call to operator()

• f(n) object may have state• f(n) object has its own type• f(n) object is usually quicker than a function pointer.

– Lambdas• Convenient method of specifying algorithm and function details• Very simple way of defining some behavior that may only be done is some other cryptic

fashion

Page 8: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++• Iterators

Iterators are objects that traverse sequences of elements and are defined with a formal API based upon element references (pointers).

• Output Iterators• Input Iterators• Forward Iterators• Bidirectional Iterators• Random Access Iterators

– Output IteratorsOutput iterators only steps forward with a write. An output iterator can only iterate over a range of elements once. Common output iterators are ostreams and inserters (e.g. cout).

– Input IteratorsInput iterators only steps forward with a read and only reads the element once. Common input iterators are istreams such as ‘cin’

– Forward IteratorsInput iterators that can be compared to another input iterator pointing to the same element. Forward lists and unordered containers provide these types of iterators.

Page 9: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++– Bidirectional Iterators

Forward iterators that can also traverse elements in the reverse or backward direction. List, maps, and set containers provide bidirectional iterators.

– Random Access IteratorsAbility to perform iterator arithmetic as well as all of the functions that bidirectional iterators perform. All of the random access containers can perform these type of iterators (i.e. array, vector, deques and strings).

Page 10: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

Page 11: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• AlgorithmsC++ Algorithms process iterator ranges and allow operations passed into the algorithm that will be used internally. The ranges for iterators must be valid which is usually specified as .begin() and .end() of the same iterator.

1. Modifying, Removing, and Non-modifying algorithms2. Sorting, Mutating, and Sorted-range algorithms3. Numeric algorithms

– Modifying, Removing, and Non-modifying algorithmsModifying algorithms all change in values of elements whereas the Non-modifying algorithms do not. In general Non-modifying algorithms return some predicate return value or something about the element(s) (e.g. find_if() or count() )

– Sorting, Mutating, and Sorted-range algorithmsMutating algorithms change the order of elements by changing the values of the elements (e.g. assigning new values or swapping values). Sorting algorithms are special cases of mutating algorithms in that the order of elements are changed but not due to changing values of elements rather the indexes of where the elements can be found change.

Page 12: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++– Numeric algorithms

Algorithms that combine the values of numeric elements in various ways. For instance, accumulate() will process the sum of all of the elements.

• Specific Algorithms– for_each()

template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function fn);• first, last Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.• fn Unary function that accepts an element in the range as argument. This can either be a function pointer or a move constructible function object. Its return value, if any, is ignored.

– count()template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val);

Page 13: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++

• first, last Input iterators to the initial and final positions of the sequence of elements. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.• val Value to match. T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).

– find()template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

• first, last Input iterators to the initial and final positions in a sequence. The range searched is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.• val Value to search for in the range. T shall be a type supporting comparisons with the elements pointed by InputIterator using operator== (with the elements as left-hand side operands, and val as right-hand side).

Page 14: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++– search()

template <class ForwardIterator1, class ForwardIterator2> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred);

• first1, last1 Forward iterators to the initial and final positions of the searched sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.• first2, last2 Forward iterators to the initial and final positions of the sequence to be searched for. The range used is (first2,last2). For (1), the elements in both ranges shall be of types comparable using operator== (with the elements of the first range as left-hand side operands, and those of the second as right-hand side operands).pred Binary function that accepts two elements as arguments (one of each of the two sequences, in the same order), and returns a value convertible to bool. The returned value indicates whether the elements are considered to match in the context of this function. The function shall not modify any of its arguments. This can either be a function pointer or a function object.

Page 15: C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,

C++– remove()

template <class ForwardIterator, class T> ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);

• first, last Forward iterators to the initial and final positions in a sequence of move-assignable elements supporting being compared to a value of type T. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.• val Value to be removed.

– sort()template <class RandomAccessIterator> void sort (RandomAccessIterator first, RandomAccessIterator last);template <class RandomAccessIterator, class Compare> void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);first, last Random-access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. RandomAccessIterator shall point to a type for which swap is properly defined and which is both move-constructible and move-assignable.