std::flat_set Member Functions
The std::flat_set class contains the function to perform different tasks some of which are as follows:
Iterators
Iterators are used to access the elements of the flat_set container or iterate through the container. The following member functions are used to get the iterator:
S.No |
Function |
Description |
---|---|---|
1 |
begin() | It returns the iterator to the first element in the flat_set. |
2 |
end() | It returns the iterator to the last elements in the flat_set. |
3 |
rbegin() | It returns the reverse iterator to the last element. |
4 |
rend() | It returns the reverse iterator to the first element. |
Example 1: Traversing flat set container elements using iterators.
C++
// C++ program to illustrate the flat_set traversal #include <flat_set> #include <iostream> using namespace std; // driver code int main() { // initializing using initializer list flat_set< int > fset{ 4, 8, 1, 9, 2 }; // traversing using loops cout << "Elements in the fset container:\n" ; for ( auto i = fset.begin(); i != fset.end(); i++) { cout << *i << " " ; } return 0; } |
Output
Elements in the fset container: 1 2 4 8 9
Capacity
These functions are used to return the size-related values.
S.No. |
Function |
Description |
---|---|---|
1 |
empty() | It returns true if the set is empty, otherwise returns false. |
2 |
size() | It returns the number of elements currently present in the flat set. |
3 |
max_size() | It returns the maximum possible size of the flat set. |
Example 2: C++ Program to find the size and max_size of the flat set container.
C++
// C++ program to find the size and allocated size of the // flatset container #include <cstdlib> #include <flat_set> #include <iostream> using namespace std; // driver code int main() { // initializing using initializer list flat_set< int > fset{ 4, 8, 1, 9, 2 }; // checking if the fset is empty if (fset.empty()) { cout << "The fset is empty" ; exit (0); } // traversing using loops cout << "Elements in the fset container:\n" ; for ( auto i = fset.begin(); i != fset.end(); i++) { cout << *i << " " ; } printf ( "\n" ); // Size of the container cout << "Size of the Container: " << fset.size() << endl; cout << "Maximum Possible size of the container: " << fset.max_size() << endl; return 0; } |
Output
Elements in the fset container: 1 2 4 8 9 Size of the Container: 5 Allocated size of the container: 1073741823
Note: The maximum possible size returned by the max_size() function may change depending on the machine and implementation.
Modifiers
The modifier member functions are used to insert, remove and update the flat set container elements. Some of them are as follows:
S. No. |
Function Name |
Description |
Time Complexity |
---|---|---|---|
1. |
emplace() | It is used for the in-place insertion of the element in the container. |
O(n) |
2. |
insert() | It is used for the simple insertion of the element in the flat set. |
O(n) |
3. |
erase() | It is used to remove the element or a range of elements from the flat set. |
O(n) |
4. |
swap() | It is used to swap two flat sets of the same type |
O(1) |
5. |
clear() | It is used to clear all the elements of the flat_set container |
O(n) |
Example 3: C++ Program to insert, remove, and clear data in std::flat_set Container
C++
// C++ program to illustrate the use of insert(), erase(), // clear(), emplace() functions of std::flat_set class #include <flat_set> #include <iostream> using namespace std; // utility function to print the flat_set void printfset(flat_set< int >& fset) { for ( auto i : fset) { cout << i << " " ; } cout << endl; } // driver code int main() { flat_set< int > fset{ 4, 8, 1, 9, 2 }; cout << "Initial Elements in fset: " ; printfset(fset); // inserting elements fset.emplace(3); fset.insert(5); cout << "Elements in fset after Insertion: " ; printfset(fset); // deleting elements fset.erase(8); fset.erase(9); cout << "Elements in fset after Deletion: " ; printfset(fset); // clearing the fset container cout << "Size of fset: " << fset.size() << endl; fset.clear(); cout << "Size of fset after clear(): " << fset.size(); return 0; } |
Output
Initial Elements in fset: 1 2 4 8 9 Elements in fset after Insertion: 1 2 3 4 5 8 9 Elements in fset after Deletion: 1 2 3 4 5 Size of fset: 5 Size of fset after clear(): 0
Basic flat_set Operations
These functions provide the basic flat set operations:
S. No. |
Function |
Description |
Time Complexity |
---|---|---|---|
1. |
find() | Searches the flat_set for the given element and returns an iterator to it. If not found, returns the iterator to the end. |
O (logn) |
2. |
count() | Returns the number of occurrences of an element in the flat_set. Since flat_set only stores unique elements, the count can be either 0 or 1. |
O (logn) |
3. |
contains() | It returns true if the flat set contains the given element, otherwise, returns false. |
O (logn) |
4. |
lower_bound() | It returns the iterator to the first element which is equivalent or greater than the given value |
O (logn) |
5. |
upper_bound() | It returns the iterator to the first element which is equal to or greater than the given element. |
O (logn) |
C++ 23 – Header
<flat_set> is a header file that provides the implementation of the std::flat_set container class in C++, which is a sorted associative container that stores unique elements and allows for fast access, insertion, and removal of elements.
The flat_set is a container similar to the set but its elements are stored contiguously in memory. This can provide faster iteration times and better cache locality compared to std::set, especially when the container is relatively small or its elements are trivially copy-able.