multiset find() function in C++ STL
Last Updated :
07 Jul, 2022
The multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set.
Syntax:
multiset_name.find(element)
Parameters: The function accepts one mandatory parameter element which specifies the element to be searched in the multiset container.
Return Value: The function returns an iterator which points to the element which is searched in the multiset container. If the element is not found, then the iterator points to the position just after the last element in the multiset.
Time complexity: If n is the size of multiset, then the time complexity of multiset::find() function is logarithmic order of n i.e. O(log(n)).
Below program illustrates the above function.
Program 1:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< int > s;
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(5);
s.insert(3);
s.insert(3);
s.insert(3);
s.insert(5);
cout << "The set elements are: " ;
for ( auto it = s.begin(); it != s.end(); it++)
cout << *it << " " ;
auto pos = s.find(3);
cout << "\nThe set elements after 3 are: " ;
for ( auto it = pos; it != s.end(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
The set elements are: 1 2 3 3 3 4 5 5
The set elements after 3 are: 3 3 3 4 5 5
Program 2:
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset< char > s;
s.insert( 'a' );
s.insert( 'a' );
s.insert( 'a' );
s.insert( 'b' );
s.insert( 'c' );
s.insert( 'a' );
s.insert( 'a' );
s.insert( 'c' );
cout << "The set elements are: " ;
for ( auto it = s.begin(); it != s.end(); it++)
cout << *it << " " ;
auto pos = s.find( 'b' );
cout << "\nThe set elements after b are: " ;
for ( auto it = pos; it != s.end(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
The set elements are: a a a a a b c c
The set elements after b are: b c c
Share your thoughts in the comments
Please Login to comment...