Data Structure FF - Red
Data Structure FF - Red
Report Accepted:
Mark (Score):
Individual activity
« Set, multiset »
REPORT
(sign ,date)
2
«CONTENTS»
• Introduction……………………………………………………………3
1. What is Set?………………………………………………….....3
2. Properties of Set………………………………………………..6
3. Examples about Set………………………………………….....7
4. Example problems: N-1……………………………………….10
N-2………………………………………………………………11
5. What is Multiset?......…………………………………………..13
6. Properties of Multiset………………………………………….14
7. Example about Multiset………………………………………..15
8. Example problems: N-1………………………………………...16
• Result ………………………………………………………………….18
• References……………………………………………………………..19
3
«INTRODUCTION»
What is set?
Programming languages like C++ offer sets as part of their standard libraries,
allowing programmers to work with these collections seamlessly. Python, Java,
and many other languages have their versions of sets too. Sets are often
implemented using hash tables or balanced trees, which make operations like
insertion, deletion, and searching quite efficient, even for large sets. Sets maintain
an order based on a specific criterion—usually, elements are sorted in ascending
order by default. This ordering makes certain operations easier to perform. Sets
support various operations like adding elements, removing elements, checking for
the existence of an element, and performing set operations like union, intersection,
and difference. Iterators allow you to loop through the elements of a set, which is
super useful for processing data or performing specific actions on each unique
item. Think of a unique list of student IDs in a class or tracking unique email
addresses in a database—sets are perfect for scenarios where you need distinct
values without any repetition. The efficiency of sets comes from their ability to
perform operations swiftly, especially in cases where you need to quickly
4
determine if an element exists or add new unique items. Sets are quite different
from multisets (or bags), as multisets allow duplicates while sets strictly maintain
uniqueness. Sets are like your go-to toolbox. They're handy for all sorts of tasks—
whether you're removing duplicates from a list or checking for unique elements in
a dataset. Sets are an elegant and efficient way to manage collections of unique
elements in programming. They provide a structured and ordered approach to
handling data, ensuring each value appears only once.
An associative container that contains a sorted set of unique objects pf type Key.
Template<
class key,
class Allocator=std::<Key>,
>class set;
Set functions
size()
• clear()
• count(),find()
5
• empty()
• insert(key),insert(it1,it2),insert(init_list)
• erase(it),erase(it1,it2),erase(key)
• upper_bound(key),lower_bound(key)
• =
Insert()
Insert()
insert(Key key)
• Insert key
• Returns pair consisting of iterator to inserted element (or element present)
and bool set to true if insertion took place
insert(iterator pos1,iterator pos2)
• Inserts elements in range(pos1,pos2)
• Returns nothing(void)
insert(initializer_list l)
• Inserts elemenents from initilazer list
• Returns nothing(void)
Erase()
Erase(iterator pos)
Erase(iterator pos1,pos2)
6
Erase(key)
Properties:
Ordered collection: Elements in a set are sorted in a specific order defined by the
sorting criterion. By default, it uses the less-than operator (operator<) for ordering.
Uniqueness: Each element in a set is unique, meaning duplicates are not allowed.
When inserting an element that already exists, it won't be added again.
Dynamic Sizing: Sets are dynamic in size, meaning elements can be inserted and
removed at runtime. The size of the set changes based on the operations performed.
Search and Access: Elements can be efficiently searched within a set using its
member functions like find() which has a time complexity of O(log n) for ordered
sets.
Example 1:
We started with a group of numbers in a special order, added more numbers to it,
and saw how it keeps only unique ones, ignoring repeats.
Example 2:
We began by creating a set filled with different integers. We checked the size of
the set before and after adding elements. Then, we showcased the unique numbers
within it. Afterward, we removed a particular value and showed the modified set.
8
Example problems:
N-1
Code:
11
Explanation:
This program in C++ counts how many different numbers you type in. It keeps
track of the unique numbers you enter using something called a "set." First, it asks
how many sets of numbers you want to type. Then, for each set, it asks how many
numbers you'll type and then records those numbers. Finally, it shows you how
many different numbers you entered altogether.
N-2
12
Code:
Explanation:
This program in C++ collects different numbers you type in and shows them back
to you. It asks how many numbers you want to type. Then, you type those numbers
one by one, and it remembers only the different ones. After you finish typing, it
shows all the different numbers you entered.
13
What is Multiset?
Often implemented using balanced binary search trees (like Red-Black Trees)
or hash tables.
Operations on Multiset:
Example Problems:
N-1
17
Code:
18
«RESULT»
std::set
Think of std::set like a box for toys. It keeps only one of each toy and puts them in
order, like arranging toys from small to big. So, if you have the same toy twice, it
won't go in. It's helpful when you want things to stay organized and need to find
something quickly.
std::multiset
Now, std::multiset is more like a box where you can put as many same toys as you
want. It also keeps toys in order, so finding a specific toy is still easy. It's handy
when you're okay with having lots of the same toy and still want them sorted.
«RECOMMENDATION»
• Easy to look through: You can easily go through all the toys inside.:
• Same tools: They both have similar tools to add, remove, or find toys,
making them easy to use.
• Part of the Toy Collection: They're part of the same toy collection kit, so
they work the same way in any toy set.
19
• Useful in different situations: You can use them in different toy situations,
whether you want an organized collection with no duplicates or a sorted one
that allows many of the same toy.
• Can change how they sort: You can use them in different toy situations,
whether you want an organized collection with no duplicates or a sorted one
that allows many of the same toy.
«REFERENCES»
• www.geeksforgeeks.org
• www.scaler.com
• www.w3schools.com
• www.eolymp.com
• www.wikipedia.org