C++ is the best platform for developing various applications, games, OS, and software. It provides many functions by which we can do our work more easily and precisely. When we talk about the function of the C++ language, it also facilitates us with the “set find()” function. The element with the specified value can be located using the C++ set “find()” function. We pass the value that we want to be searched for as the parameter of the “find()” function. This function accepts only one parameter. It produces an iterator that points to the element if it locates it. If not, it returns an iterator that points to the set’s end. It is a built-in function in C++ programming. We utilize this set “find()” function in our codes here.
Example 1:
The “iostream” and “set” are the header files that we included here. The “iostream” is for the “cin” and “cout” functions, and the “set” is included since we have to work with the sets here. After this, we add the “namespace std”. Then, we call the “main()” function. Below this, we initialize a “set” of “int” data types and insert the values. We insert “12, 45, 80, 55, and 98” in this set. Now, we place an “auto” keyword and utilize the “set find()” function in which we add “45” as the parameter. Now, it finds this “45” number from “my_set” and displays it as we utilize the “cout”.
Code 1:
#include <set>
using namespace std;
int main(void) {
set<int> my_set = {12, 45, 80, 55, 98};
auto it = my_set.find(45);
cout << "Iterator points to the number " << *it << endl;
return 0;
}
Output:
It finds “45” in the given set and displays it here. It doesn’t show any output here, and the code ends if the value is not contained inside the set.
Example 2:
We include the “iostream” and “set” header files. The “set” is added since we have to operate with sets in this instance, and the “iostream” is for the “cin” and “cout” functions. Next, we execute the “main()” method after adding the “namespace std”. A “set” of “char” data types is initialized, and the characters are added. The characters that we insert here are “a”, “e”, “i”, “o”, and “u”. After this, we make use of the “set find()” function and pass “z” here. Below this, we place an “if” condition and then utilize the “cout” after this. If the given condition is satisfied, it prints the line that is given after this condition. But if the condition is not true, move ahead and execute the “else” part.
Code 2:
#include <set>
using namespace std;
int main(void) {
set<char> char_set = {'a', 'e', 'i', 'o', 'u'};
auto it = char_set.find('z');
if ( it == char_set.end() ) {
cout<<"The given character not found here ";
}
else {
cout << "Iterator points to the given character " << *it<< endl;
}
return 0;
}
Output:
Since we inserted the vowels inside the “set” and in the “find()” function, we add “z” to search from this set. This set cannot contain “z”, so it returns the “The given character not found” message.
Example 3:
The set that we initialize here is the “int_set” of the “int” data type. Then, we place the “iterator” here. After this, we place the “for” loop and utilize the “insert” method here in which we add “i*10”. So, when this loop starts, it multiplies the value of “i” with “10” and the value of “i” will increment at every loop cycle. Below this, we utilize the “set find()” function where the set name is “int_set” and then place “20” here. It finds “20” from the set with the help of this “find()” method and save it inside the “it” variable.
After this, we utilize the “set.erase()” function and pass the “it” variable as its parameter. So, it erases the number “20” from the set. This previous loop aids in inserting the values inside the set, and it shops the inserting values when the condition is not satisfied. It also removes “50” from the values of the set after finding it as we also utilize the “find()” and “erase()” functions below this. But this time, we insert “50” to find and erase it from the set values.
Now, it displays all the remaining values of the set as we utilize the “cout” ahead. All these values are displayed with the help of the “for” loop in which we place the “begin()” and “end()” functions. We print the values below this.
Code 3:
#include <set>
using namespace std;
int main () {
set<int> int_set;
set<int>::iterator it;
for (int i = 1; i <= 15; i++) int_set.insert(i*10);
it = int_set.find(20);
int_set.erase (it);
int_set.erase (int_set.find(50));
cout << "my integer set contains:";
for (it = int_set.begin(); it!=int_set.end(); ++it)
cout << ' ' << *it;
cout << endl;
return 0;
}
Output:
We might notice that “20” and “50” are not displayed here since we searched and erased those values from the set in the previous code by utilizing the “find()” and “erase()” functions.
Example 4:
The “bits/stdc++.h” header file is included this time, so we don’t need to add more header files here as it contains all the standard libraries. Then, after invoking the “main()”, we create a set named “new_s” of the “int” data type. After this, we insert the values separately in this set using the “insert()” function with the name of the set that we created here.
After this, we place the “auto” keyword and place the “int_pos” variable and initialize it with the “find()” function. Here, we pass “43”. So, it finds “43” from the set. Then, the iterator here is now pointing to the location where this “43” is placed. After this, we print all the values that are present in this set after the value of “43”. The values of the set are saved in a sorted way by default.
Code 4:
using namespace std;
int main()
{
set<int> new_s;
new_s.insert(21);
new_s.insert(43);
new_s.insert(24);
new_s.insert(65);
new_s.insert(83);
new_s.insert(99);
auto int_pos = new_s.find(43);
cout << "The set elements after 43 are: ";
for (auto it = int_pos; it != new_s.end(); it++)
cout << *it << " ";
return 0;
}
Output:
Here, it finds “43” from the set and then displays all the numbers in the set. The set numbers are sorted and displayed in a sorted manner here.
Example 5:
After adding the header file and the “std” namespace, the “main()” function is then called here and a “set” of the “char” data type is produced. Next, we use the “insert()” function with the name of the set that we constructed here to enter each value independently into this set. Subsequently, we insert the term “auto” and create a variable called “char_pos”, initializing it using the “find()”function.
Now that we passed “m”, it retrieves “m” from the set and the iterator points to the position of this “m” at this moment. If the “m” character is found in this “set”, it displays its position here. We get this position of the character by utilizing the “distance()” function as it aids in finding the position between the two iterators.
Code 5:
using namespace std;
int main()
{
set<char> intSet;
intSet.insert('a');
intSet.insert('m');
intSet.insert('p');
intSet.insert('o');
intSet.insert('z');
auto char_pos = intSet.find('m');
if (char_pos != intSet.end())
cout << "character found at position: "
<< distance(intSet.begin(), char_pos) << endl;
else
cout << "Character not present in the set";
return 0;
}
Output:
This result shows that the desired character is found in the given set and the position of this set is “1”.
Conclusion
We explored the “set find()” function examples in this code. We discussed that within C++, a built-in function called “set find()” returns an iterator to the element that is searched within the set container. If the element cannot be located, the iterator indicates the location immediately following the final element in the set. We also demonstrated the examples here and utilized this function in this guide.