Set Basics
Basic Set Operations in C++
C++ Set Basics Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <set>
using namespace std;
int main() {
// Create set
set<int> numbers;
// Insert elements
numbers.insert(5);
numbers.insert(2);
numbers.insert(8);
numbers.insert(1);
numbers.insert(5); // Duplicate, will be ignored
numbers.insert(3);
// Display set
cout << "Set elements (sorted and unique): ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// Check if element exists
if (numbers.find(5) != numbers.end()) {
cout << "Element 5 found in set" << endl;
}
// Count occurrences (0 or 1 for set)
cout << "Count of 5: " << numbers.count(5) << endl;
cout << "Count of 10: " << numbers.count(10) << endl;
// Size
cout << "Set size: " << numbers.size() << endl;
// Erase element
numbers.erase(5);
cout << "\nAfter erasing 5: ";
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// Lower and upper bound
set<int> s = {1, 2, 3, 5, 7, 9};
auto it_low = s.lower_bound(4); // First element >= 4
auto it_up = s.upper_bound(6); // First element > 6
cout << "\nLower bound of 4: " << *it_low << endl;
cout << "Upper bound of 6: " << *it_up << endl;
return 0;
}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
Understanding Set Basics
This program teaches you how to use Set Basics in C++. Set is a container from the STL that stores unique elements in automatically sorted order. Duplicates are automatically removed, making sets perfect for maintaining sorted, unique collections.
---
1. What This Program Does
The program demonstrates basic set operations:
Sets provide efficient storage of unique, sorted elements.
---
2. Header Files Used
---
3. Understanding Sets
Set Concept
:
Key Features
:
---
4. Creating Sets
Declaration
:
set<int> numbers;
How it works
:
---
5. Inserting Elements
Using insert()
:
numbers.insert(5);
numbers.insert(2);
numbers.insert(8);
numbers.insert(5); // Duplicate ignored
How it works
:
Result
: {1, 2, 3, 5, 8} (sorted, unique)
---
6. Finding Elements
Using find()
:
auto it = numbers.find(5);
if (it != numbers.end()) {
}
// Element foundHow it works
:
---
7. Counting Occurrences
Using count()
:
int count = numbers.count(5); // Returns 1 or 0
How it works
:
---
8. Erasing Elements
Using erase()
:
numbers.erase(5);
How it works
:
---
9. 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
:
---
10. When to Use Sets
Best For
:
Example Scenarios
:
---
11. Important Considerations
Automatic Sorting
:
Uniqueness
:
Performance
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning associative containers, understanding unique data storage, and preparing for advanced algorithms and data structures in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Set Basics
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.