Now Reading: Set Find() Function Example in C++

Loading
svg
Open

Set Find() Function Example in C++

January 21, 20241 min read

C++ is the best platform to develop various applications, games, OS and software. It provides many functions so that we can do our work with more ease and accuracy. When we talk about functions of C++ language, it also gives us the facility of “set find()” function. The element with the specified value can be located using the C++ set “find()” function. We pass the value we want to search as a parameter to the “find()” function. This function accepts only one parameter. This generates an iterator that points to the element when it is detected. If not, it returns an iterator pointing to the end of the set. It is a built-in function in C++ programming. We use this set “find()” function in our code here.

Example 1:

“iostream” and “set” are the header files that we have included here. “iostream” is for the “syn” and “cout” functions, and “set” is included because we have to work with sets here. Next, we add “namespace std”. Then, we call the “main()” function. Below this, we initialize a “set” of “int” data types and insert the values. We put “12, 45, 80, 55, and 98” in this set. Now, we put an “auto” keyword and use the “set find()” function in which we add “45” as a parameter. Now, it finds this number “45” from “my_set” and displays it as we use “cout”.

Code 1:

#include
#Involved <सेट>
using namespace std;
int main,void, ,
set<int here, my_set= ,12, 45, 80, 55, 98,,
auto this = my_set.find,45,,
court , “iterator points to number” , ,it , Andal;
return 0,
,

Output:

It finds “45” in the given set and displays it here. It shows no output here, and the code terminates if the value is not included inside the set.

Example 2:

We include “iostream” and “set” header files. “set” is added because we have to work with sets in this example, and “iostream” is for the “syn” and “cout” functions. Next, we execute the “main()” method after adding “namespace std”. A “set” of “four” data types is initialized, and characters are added. The letters we put here are “a”, “e”, “i”, “o”, and “u”. Next, we use the “set find()” function and pass “z” here. Below this, we place an “if” condition and then use “cout” after it. If the given condition is satisfied, it prints the line which is followed by this condition. But if the condition is not true, go ahead and execute the “else” part.

Code 2:

#include
#Involved <सेट>
using namespace std;
int main,void, ,
set<Four, char_set= ,'A', 'I', 'I', 'O', 'You',,
auto it = char_set.find,'z',,
If , this == char_set.end,, , ,
court,“The given character is not found here”,
,
Other ,
court , “Iterator points to the given character” , ,it, Andal;
,
return 0,
,

Output:

Since we put the vowels inside a “set” and into the “find()” function, we add “z” to search from this set. This set cannot contain “z”, so it returns the message “The given character is not found”.

Example 3:

The set we initialize here is “int_set” of data type “int”. Then, we put the “repeater” here. Next, we put a “for” loop and use the “insert” method here in which we add “i*10”. So, when it starts the loop, it multiplies the value of “i” by “10” and the value of “i” will increase on each loop cycle. Below this, we use the “set find()” function where the name of the set is “int_set” and then put “20” in here. It finds “20” from the set with the help of this “find()” method and saves it inside “it” variable.

Next, we use the “set.erase()” function and pass the “it” variable as its parameter. So, this erases the number “20” from the set. This previous loop helps in inserting values ​​inside the set, and it stores the inserted values ​​if the condition is not met. It also removes “50” from the values ​​of the set after finding it as we also use “find()” and “erase()” functions below it. But this time, we put “50” to find and erase it from the specified values.

Now, as we further use “cout”, it displays all the remaining values ​​of the set. All these values ​​are displayed with the help of “for” loop in which we place the functions “begin()” and “end()”. We print the value below it.

Code 3:

#include
#Involved <सेट>
using namespace std;
int main ,, ,
set<int here, int_set;
set<int here,::it repeater;
For ,integer i = 1, I <, 15, i++, int_set.insert,I,10,,
this = int_set.find,20,,
int_set.erase ,it,,
int_set.erase ,int_set.find,50,,,
court , “My integer set contains:”,
For ,this = int_set.begin,,, it,=int_set.end,,, ++this,
court , , , ,it;
court , Andal;
return 0,
,

Output:

We can see that “20” and “50” are not displayed here because we have searched and erased those values ​​from the set in the previous code using “find()” and “erase()” functions.

Example 4:

This time the “bits/stdc++.h” header file is included, so we do not need to add more header files here as it includes all the standard libraries. Then, after invoking “main()”, we create a set called “new_s” of “int” data type. Next, we insert different values ​​into this set using the “insert()” function with the name of the set created here.

Next, we insert the “auto” keyword and the “int_pos” variable and initialize it with the “find()” function. Here, we pass “43”. So, it finds “43” from the set. Then, the repeater here is now pointing to the location where this “43” is placed. Next, we print all the values ​​in this set after the value “43”. Values ​​in sets are saved in sorted order by default.

Code 4:

#includes
using namespace std;
int main,,
,
set<int here, news;
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,,
court , “The set elements after 43 are:”,
For ,auto this = int_pos; it ,= new_s.end,,, this++,
court , ,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 here in a sequential manner.

Example 5:

After adding the header file and the “std” namespace, the “main()” function is called here and a “set” of “char” data type is created. Next, we use the “Insert()” function with the name of the set that we created here to insert each value in this set independently. Next, we insert the word “auto” and create a variable called “char_pos”, initialize it using the “find()” function.

Now that we have passed “m”, it retrieves “m” from the set and the iterator points to the position of this “m” at this time. If the character “m” is found in this “set”, it displays its position here. We get this position of the character by using “distance()” function as it helps in finding the position between two iterations.

Code 5:

#includes
using namespace std;
int main,,
,
set<Four, 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,,,
court , “character found at location:”
, distance,intSet.begin,,char_pos, , Andal;
Other
court , “character does not exist in set”,
return 0,
,

Output:

This result indicates that the desired character has been found in the given set and the state 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 over the element searched within the set container. If the element cannot be located, the iterator points to the location immediately after the last element in the set. We have also demonstrated examples and uses of this function in this guide.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg