0% found this document useful (0 votes)
57 views

Sets and Multisets in C++

This document discusses C++ sets and multisets. It defines sets as containers that store unique elements in sorted order using red-black trees. Multisets allow duplicate elements. The document outlines properties of sets, how they work internally, common operations like insertion and deletion, how to initialize and traverse sets using iterators, and provides code examples. It concludes by sharing a GitHub link with more set projects.

Uploaded by

Michael Workineh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Sets and Multisets in C++

This document discusses C++ sets and multisets. It defines sets as containers that store unique elements in sorted order using red-black trees. Multisets allow duplicate elements. The document outlines properties of sets, how they work internally, common operations like insertion and deletion, how to initialize and traverse sets using iterators, and provides code examples. It concludes by sharing a GitHub link with more set projects.

Uploaded by

Michael Workineh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ADDIS ABEBA

SCIENCE AND TECHNOLOGY


UNIVERSITY
COLLEGE OF ELECTRICAL AND MECHANICAL ENGINEERING

DEPARTMENT OF SOFTWARE ENGINEERING

FUNDAMENTALS OF PROGRAMING ASSIGNMENT

Group members ID

1.Meseret Bolled ETS 1052/14

2.Michael Workneh ETS 1058/14

3.Michael Addis ETS 1060/14

4.Michael Getu ETS 1061/14

5.Michael Siamregn ETS 1070/14

Submitted To: Lec. Chere Lema

Section: C

Sets and Multi-sets


What are Sets?

Sets in C++ are a kind of STL containers that are used for storing elements in a
sorted way. The elements are ordered to a strict weak ordering in a set type container
and since this elements are constant we cannot manipulate the values of the existing
elements in the set. Sets offer high performance sorting, ordering, and membership
testing, sets underpin solutions for diverse problems. This essay will dive deep into C++
sets - their technical internals, syntax, capabilities, use cases, and more. multisets
permit duplicates. These containers are implemented as ordered binary trees, ensuring
efficient searching, insertion, and deletion operations.

Ordered sets implement a red-black tree structure internally. The elements are sorted
according to a comparator function, allowing rapid insert, delete, and search in
logarithmic O(log n) time.

Unordered sets use hashing with buckets, giving average O(1) time but losing ordering.

Properties of sets

● Elements are stored in a sorted manner.


● Each element that is stored is unique.
● There is no manipulation of elements only deletion and addition.

How they work

● Internally, set containers are implemented as red-black trees which enables efficient
sorting and searching in O(log n) time.
● Elements are ordered using the < comparison operator which must be defined for the
element type.
● It maintains the ordering by inserting elements in the correct position based on the
comparator.
● Duplicate elements are not allowed in std::set but allowed in std::multiset.

When are sets used?

Sets are used in competitive programming where we need to store values of a


component in a sorted manner but without the need for manipulation of those values

● When you need to store unique/sorted data and access it efficiently. The automatic
sorting and removal of duplicates is convenient.
● When you need fast search, insert, delete operations on sorted data.
● As an alternative to std::map when you just need to store keys but not key-value pairs.
● When you need a sorted collection of elements but don't want to call sort() explicitly
whenever elements are added/removed.

Some applications
● Storing sorted lists of unique elements like names, grades, etc.
● Membership testing - fast checking if element exists already or not.
● Mathematical set operations like unions, intersections, differences etc.
● Removing duplicates efficiently from a collection.

How are sets implemented?

Sets are traversed using iterators, so we must include the needed header files to work
with sets which could be

Syntax

Usage and Operations

Set containers are typically employed when you need to store a collection of unique elements,
ensuring that no duplicates exist. On the other hand, multiset containers allow duplicate
elements, making them suitable for scenarios where multiple occurrences of the same value are
expected.
Common Operations

Insertion: Adding elements to a set or multiset can be done using the insert() member function.

Deletion: Elements can be removed using the ‘erase()’ member function.

Searching: You can search elements using ‘find()’ member functionality.


How are sets initialized?

We define the type of values to be stored in a set at the time of initialization in C++. with
default sorting from smallest to largest. Sets can be initialized in many kinds of ways as well
with some being:

● Empty set
● set with values
● set initialized with another set
● set initialized with an array

What kind of Iterators are used?


Ordered sets provide bidirectional iterators for traversing elements in ascending order.
Unordered sets offer forward iterators. Validity is generally maintained except after rehashing or
erasing elements for unordered sets.

In C++, iterators are essential for traversing and manipulating elements in various containers,
including sets and multisets. For sets and multisets, we primarily use begin() and end() iterators.

Iterator Usage

Traversal: To iterate through the elements in a set or multiset, use a for loop with iterators.

Insertion with Iterators: you can insert elements at a specific position using iterators

Some Code examples include:


code1:
code 2:
code 3:
github link: https://github.com/SWEG-2015-EC-Batch/THE-IT-CROWD/tree/main/FoP-II/
PROJECTS/Set%20and%20Multi%20Set%20Containers

You might also like