- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
C++ Sets
In C++, sets are associative containers that store unique elements following a specific order. The std::set is part of the Standard Template Library (STL) and is typically implemented as a balanced binary search tree (often a Red-Black Tree). Here’s a breakdown of how to use std::set:
Basic Operations with std::set
Include the Necessary Header:
cpp#include#include Declare a Set:
cppstd::set<int> mySet;By default,
std::setstores elements in ascending order. You can also specify a custom comparator to change the order.Add Elements:
cppmySet.insert(5); mySet.insert(10); mySet.insert(3);Note:
std::setautomatically ignores duplicate elements. If you try to insert an element that already exists, it will not be added again.Check for Presence of an Element:
cppif (mySet.find(10) != mySet.end()) { std::cout << "10 is in the set." << std::endl; }Remove Elements:
cppmySet.erase(5); // Removes the element with value 5Iterate Through the Set:
cppfor (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl;Get the Size of the Set:
cppstd::cout << "Size of the set: " << mySet.size() << std::endl;Check if the Set is Empty:
cppif (mySet.empty()) { std::cout << "Set is empty." << std::endl; }
Custom Comparator
If you want to store elements in a custom order, you can provide a comparator:
Define a Custom Comparator:
cppstruct Compare { bool operator()(int a, int b) const { return a > b; // Descending order } };Declare a Set with the Custom Comparator:
cppstd::set<int, Compare> customSet;
Example Code
Here's a complete example illustrating basic operations and a custom comparator:
cpp#include
#include
// Custom comparator for descending order
struct Compare {
bool operator()(int a, int b) const {
return a > b; // Descending order
}
};
int main() {
// Default set (ascending order)
std::set<int> mySet = {5, 10, 3, 7, 7};
std::cout << "Default set:" << std::endl;
for (const auto& elem : mySet) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Set with custom comparator (descending order)
std::set<int, Compare> customSet = {5, 10, 3, 7};
std::cout << "Custom set (descending order):" << std::endl;
for (const auto& elem : customSet) {
std::cout << elem << " ";
}
std::cout << std::endl;
// Check presence and remove elements
if (mySet.find(10) != mySet.end()) {
std::cout << "10 is in the default set." << std::endl;
}
mySet.erase(10);
std::cout << "After removing 10, default set:" << std::endl;
for (const auto& elem : mySet) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Output
vbnetDefault set:
3 5 7 10
Custom set (descending order):
10 7 5 3
10 is in the default set.
After removing 10, default set:
3 5 7
This example demonstrates the basic usage of std::set, including iteration, custom ordering, and element management.