In this post, we will explore the process of navigating through a set using const_iterator in C++. Prior to delving into its execution, it is essential to understand the concept of sets in C++.
What is the Set?
The STL container std::set in C++ showcases an ordered assortment of diverse elements. The internal comparison object of a set, denoted as Compare, consistently enforces a strict weak ordering rule that dictates the sorting arrangement of set members. By default, elements are organized in ascending order.
Normally, the std::set method is built using a Red-Black Tree or a different balanced binary search tree. This particular implementation ensures logarithmic time complexity for most actions such as searches, additions, and removals.
When trying to add an item that is already present in the set, the insertion operation will fail because each element within a set must be distinct.
For Instance:
Consider this set of distinct values: {5, 2, 8, 3, 1}. In the C++ programming language, the elements of a std::set are sorted automatically in ascending order. Hence, the set would be internally arranged as: {1, 2, 3, 5, 8}.
The set exclusively consists of distinct elements, meaning that attempting to insert a number already present in the set will not result in its duplication. For instance, adding the number 3 again will have no effect since it is already part of the set.
Example:
Let's consider an example to demonstrate the set data structure in C++.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
mySet.insert(3);
mySet.insert(1);
mySet.insert(4);
mySet.insert(1); // This insertion will have no effect
for (int x : mySet) {
std::cout << x << " ";
}
return 0;
}
Output:
What is the Const_iterator in C++?
A type of iterator known as a const_iterator is employed in C++ to traverse a container (like std::vector, std::list, or std::set) while preventing any modifications to the items by the iterator. This iterator is specifically utilized for iterating over the elements of a container in a mode that restricts modifications.
Example:
Let's consider a scenario to demonstrate the const_iterator in C++.
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector = {1, 2, 3, 4, 5};
// Obtain a const_iterator to the beginning of the vector
std::vector<int>::const_iterator it = myVector.begin();
// Traverse the vector using the const_iterator
while (it != myVector.end()) {
std::cout << *it << " ";
++it;
}
std::cout << std::endl;
return 0;
}
Output:
Traverse a Set with const_iterator:
Constant references to container objects are facilitated by const_iterator. This implies that the value cannot be altered, only accessed. This feature assists in managing unexpected alterations that may arise during traversal.
The four functions of the std::set class return constant iterators:
- std::set::cbegin: This function provides a const iterator that identifies the starting element of the set.
- std::set::cend: This function returns a const iterator pointing directly after the last element of the set.
- std::set::crbegin: This function returns a const reverse iterator pointing to the reverse first (or last) element of the set.
- std::set::crend: This function returns a const reverse iterator thacpp tutorials directly after the first element of the set, which is after the last element.
- First, create a std::set<int> mySet with some elements.
- After that, obtain a const_iterator at the beginning of mySet.
- Iterate over the set using the constiterator: While the constiterator is not at the end of the set: Access the elemencpp tutorialed to by the constiterator using *. Perform any desired operations on the element. Increment the constiterator to move to the next element in the set.
- End of traversal.
- While the constiterator is not at the end of the set: Access the elemencpp tutorialed to by the constiterator using *. Perform any desired operations on the element. Increment the const_iterator to move to the next element in the set.
- Access the elemencpp tutorialed to by the const_iterator using *.
- Perform any desired operations on the element.
- Increment the const_iterator to move to the next element in the set.
Pseudocode:
Example 1:
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
std::cout << "Enter integers (enter -1 to stop):" << std::endl;
// Read integers from the user and insert them into the set
int num;
do {
std::cin >> num;
if (num != -1) {
mySet.insert(num);
}
} while (num != -1);
// Print the elements of the set using a const_iterator
std::cout << "Elements in sorted order:" << std::endl;
std::set<int>::const_iterator it = mySet.begin();
while (it != mySet.end()) {
std::cout << *it << " ";
++it;
}
std::cout << std::endl;
return 0;
}
Output:
Example 2:
// C++ program to traverse the set using the const_iterator.
#include <iostream>
#include <set>
using namespace std;
// Function to display elements of the set
// using the const_iterator
void display(set<int> set1)
{
set<int>::const_iterator c_itr;
for (c_itr = set1.cbegin(); c_itr != set1.cend();
c_itr++) {
cout << *c_itr << ", ";
}
}
// Driver Code
int main()
{
// Create an empty set
set<int> set1;
// Insert 10 elements into the set
set1.insert(7);
set1.insert(1);
set1.insert(5);
set1.insert(9);
set1.insert(6);
set1.insert(4);
set1.insert(10);
set1.insert(3);
set1.insert(2);
set1.insert(8);
// Call the display() function
display(set1);
return 0;
}
Output: