
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Set find() function in C++ programming STL
In this article we are going to discuss the set::find() function in C++ STL, their syntax, working and their return values.
What is Set in C++ STL?
Sets in C++ STL are the containers which must have unique elements in a general order. Sets must have unique elements because the value of the element identifies the element. Once added a value in a set container later can’t be modified, although we can still remove or add the values to the set. Sets are used as binary search trees.
What is set::find()
find() function is an inbuilt function in C++ STL, which is defined in header file. This function is used to find an element or a value in a set container. find() returns an iterator which points to the position of the element which is searched. If the element is not present in the set, then it returns the element just after the last element of the set container.
Syntax
Set1.find(const type_t& element);
Parameter
This function accepts one parameter, i.e., element which is to be found.
Return value
This function returns an iterator which points to the element which is to be found.
Example
Input: set<int> myset = {10, 20, 40, 80, 90}; myset.find(40); Output: element found
Example
#include <bits/stdc++.h> using namespace std; int main(){ set<int> mySet; mySet.insert(10); mySet.insert(20); mySet.insert(90); mySet.insert(80); mySet.insert(40); auto temp = mySet.find(40); cout<<"Elements after 40 are: "; for (auto i = temp; i != mySet.end(); i++) cout << *i << " "; return 0; }
Output
If we run the above code then it will generate the following output −
Elements after 40 are: 40 80 90
Example
#include <iostream> #include <set> int main (){ std::set<int> mySet; std::set<int>::iterator i; for(int i=1; i<=4; i++) mySet.insert(i*2); i = mySet.find(6); mySet.erase(i); mySet.erase(mySet.find(4)); std::cout<<"elements are : "; for (i = mySet.begin(); i != mySet.end(); ++i) std::cout << ' ' << *i; std::cout << '\n'; return 0; }
Output
If we run the above code then it will generate the following output −
Elements are : 2 8
- Related Articles
- Set find() function in C++ STL
- set lower_bound() function in C++ STL
- Set emplace_hint() function in C++ STL
- Set equal_range() function in C++ STL
- Set count() function in C++ STL
- Set max_size() function in C++ STL
- Set upper_bound() function in C++ STL
- set value_comp() function in C++ STL
- Set cbegin() and cend() function in C++ STL
- Set crbegin() and crend() function in C++ STL
- map find() function in C++ STL
- Set in Dart Programming
- Set get_allocator() in C++ STL
- Set insert() in C++ STL
- set operator= in C++ STL
