![]() |
VOOZH | about |
<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.
flat_set <T, Compare, Allocator> object_name;
where,
We can initialize the flat_set object in multiple different ways. Some of them are as follows:
If we don't provide any parameter in the declaration, the default constructor is used to initialize the std::flat_set object.
flat_set <int> object_name;
We can provide the elements in the initializer list to initialize the std::flat_set object.
flat_set <int> object_name { 1,5,4,2,3,9,8 };Note: The time complexity of the initialization of flat_set using a number of elements is O (nlogn) which is required for sorting and sorted insertion.
We can initialize the std::flat_set object using the range specified by starting iterator and ending iterator.
flat_set <data_type> object_name(startingIterator, endingIterator);
The advantage of using the move assignment operator to initialize the flat_set object is that it avoids unnecessary copies of data by directly pointing the assigned object to the memory location where the data is already stored.
flat_set <datatype> object_name; object_name = move(prev_object);
It is one of the simplest ways to initialize the flat_set object using another already existing flat_set object.
flat_set <data_type> object_name; object_name = prev_object;
The std::flat_set class contains the function to perform different tasks some of which are as follows:
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. |
Output
Elements in the fset container: 1 2 4 8 9
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. |
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.
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) |
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
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) |
The below program demonstrates some of the basic operations that can be performed on a std::flat_set, such as inserting, searching, and removing elements, as well as iterating over the elements.
Output
Elements in the flat_set: 1 2 3 4 5 6 9 Number of elements in the flat_set: 7 7 is not present. AFTER INSERTING 7!! 7 is present Upper Bound: 4 Lower Bound: 3
Note: C++23 is not yet a fully released standard and therefore there is no widely available compiler that fully supports all of its features. However, some compilers may have experimental or partial support for certain C++23 features.
If you want to experiment with the latest C++23 features, you may want to consider using a compiler that supports experimental or partial features. Some popular compilers, such as GCC and Clang, offer experimental support for some C++23 features. However, keep in mind that experimental features may not be fully stable or feature-complete, and may be subject to change in future updates.
The flat_set container is implemented as a sorted associative container, which means that its elements are sorted according to a comparison function.
However, unlike the standard set container, the flat_set stores its elements in a contiguous memory block, making it more efficient in terms of cache locality and memory usage. This is achieved by using a vector as the underlying storage for the flat_set and ensuring that the elements are always sorted in the vector.
Since the flat_set uses a vector as its underlying storage, it provides constant-time access to its elements, making it faster than the standard set container in some cases. However, the flat_set may be slower than the set container when it comes to the insertion and removal of elements, especially for large data sets.