Set Basics in C++

Basic Set Operations in C++.

BeginnerSTL Containers ProgramsExample 5 of 10
set-basics.cpp
1#include <iostream>
2#include <set>
3using namespace std;
4
5int main() {
6 // Create set
7 set<int> numbers;
8
9 // Insert elements
10 numbers.insert(5);
11 numbers.insert(2);
12 numbers.insert(8);
13 numbers.insert(1);
14 numbers.insert(5); // Duplicate, will be ignored
15 numbers.insert(3);
16
17 // Display set
18 cout << "Set elements (sorted and unique): ";
19 for (int num : numbers) {
20 cout << num << " ";
21 }
22 cout << endl;
23
24 // Check if element exists
25 if (numbers.find(5) != numbers.end()) {
26 cout << "Element 5 found in set" << endl;
27 }
28
29 // Count occurrences (0 or 1 for set)
30 cout << "Count of 5: " << numbers.count(5) << endl;
31 cout << "Count of 10: " << numbers.count(10) << endl;
32
33 // Size
34 cout << "Set size: " << numbers.size() << endl;
35
36 // Erase element
37 numbers.erase(5);
38 cout << "\nAfter erasing 5: ";
39 for (int num : numbers) {
40 cout << num << " ";
41 }
42 cout << endl;
43
44 // Lower and upper bound
45 set<int> s = {1, 2, 3, 5, 7, 9};
46 auto it_low = s.lower_bound(4); // First element >= 4
47 auto it_up = s.upper_bound(6); // First element > 6
48
49 cout << "\nLower bound of 4: " << *it_low << endl;
50 cout << "Upper bound of 6: " << *it_up << endl;
51
52 return 0;
53}

Output

Set elements (sorted and unique): 1 2 3 5 8
Element 5 found in set
Count of 5: 1
Count of 10: 0
Set size: 5

After erasing 5: 1 2 3 8
Lower bound of 4: 5
Upper bound of 6: 7

What's going on

The program demonstrates basic set operations:

Creating and inserting elements
Automatic sorting and duplicate removal
Finding elements
Counting occurrences
Erasing elements
Using lower_bound() and upper_bound()

Sets provide efficient storage of unique, sorted elements.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <set>
Provides set container class.

Understanding Sets

Set Concept

:

Stores unique elements (no duplicates)
Automatically sorted
Fast search operations
Balanced binary search tree implementation

Key Features

:

Unique elements only
Automatic sorting
Efficient search (O(log n))
Ordered traversal

Creating Sets

Declaration

:

set<int> numbers;

How it works

:

Template: set<dataType>
Initially empty
Elements automatically sorted
Duplicates automatically removed

Inserting Elements

Using insert()

:

numbers.insert(5);

numbers.insert(2);

numbers.insert(8);

numbers.insert(5); // Duplicate ignored

How it works

:

insert() adds element
Duplicates automatically ignored
Elements automatically sorted
O(log n) time complexity

Result

: {1, 2, 3, 5, 8} (sorted, unique)

Finding Elements

Using find()

:

auto it = numbers.find(5);

if (it != numbers.end()) {

}

1// Element found

How it works

:

Returns iterator to found element
Returns end() if not found
O(log n) time complexity
Efficient search

Counting Occurrences

Using count()

:

int count = numbers.count(5); // Returns 1 or 0

How it works

:

Returns 1 if element exists
Returns 0 if element doesn't exist
For set, always 0 or 1 (unique elements)
O(log n) time complexity

Erasing Elements

Using erase()

:

numbers.erase(5);

How it works

:

Removes element from set
Maintains sorted order
O(log n) time complexity
Reduces size

Lower and Upper Bound

lower_bound()

:

auto it = s.lower_bound(4); // First element >= 4

upper_bound()

:

auto it = s.upper_bound(6); // First element > 6

How it works

:

lower_bound: first element >= value
upper_bound: first element > value
Useful for range queries
O(log n) time complexity

When to Use Sets

Best For

:

Maintaining unique elements
Sorted collections
Fast search requirements
Removing duplicates
Ordered data storage

Example Scenarios

:

Unique ID storage
Sorted data requirements
Fast membership testing
Removing duplicates from data

Important Considerations

Automatic Sorting

:

Elements always sorted
Order maintained automatically
Sorted by comparison operator
Cannot change sort order (use custom comparator)

Uniqueness

:

Duplicates automatically removed
Each element appears once
Inserting duplicate has no effect

Performance

:

Insert: O(log n)
Find: O(log n)
Erase: O(log n)
Balanced binary search tree

return 0;

This ends the program successfully.