Multiset Find Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multiset Find Function

Multiset Find Function

BLUF: Mastering Multiset Find Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multiset Find Function

C++ is renowned for its efficiency. Learn how Multiset Find Function enables low-level control and high-performance computing in the tutorial below.

C++ multiset find

The find function in C++ multiset is employed to search for an element with a specified value val. In case the element is located, it will provide an iterator pointing to that element; otherwise, it will return an iterator pointing to the end of the multiset, which is multiset::end.

Syntax

Example

iterator find (const value_type& val) const;                  // until C++ 11

   const_iterator find (const value_type& val) const;              //since C++ 11
         iterator       find (const value_type& val);                    //since C++ 11

Parameter

val : denotes the specific value that will be sought in the multiset data structure.

Return value

If the element is located, it will return an iterator that points to the element. In case the element is not found, it will return an iterator pointing to the end of the multiset, which is referred to as multiset::end.

Complexity

Logarithmic in size.

Iterator validity

No changes.

Data Races

The container is accessed without making any modifications to it, whether it is the constant or non-constant versions being used.

Accessing and modifying elements concurrently is considered safe when there are no mapped values being accessed.

Exception Safety

If an error is raised, the multiset container remains unaltered.

Example 1

Let's examine a straightforward example of locating the element with the specified key value:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<int> m = {100,200,300,300};

   auto it = m.find(300);

   cout << "Iterator points to " << *it << endl;

   return 0;
}

Output:

Output

Iterator points to 300

Example 2

Let's see a simple example to find the element:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<char> m = {'a', 'b', 'c', 'a'};

    auto it = m.find('e');
   
    if ( it == m.end() ) {
    // not found
     cout<<"Element not found";
    } 
    else {
        // found
        cout << "Iterator points to " << *it<< endl;
    }
    
   return 0;
}

Output:

Output

Element not found

The find method searches for the key value "e" within the multiset "m". If the key is not present in the multiset, it will return a message indicating that the key was not found. Otherwise, it will display the contents of the multiset.

Example 3

Let's see a simple example:

Example

#include <iostream>
#include <set>
 
using namespace std;

int main()
{
    char n;
    multiset<char> example = {'a','b','c','d','b'};
    
    cout<<"Enter the element which you want to search: ";
    cin>>n;
 
    auto search = example.find(n);
    if (search != example.end()) {
        cout << n<<" found and the value is " << *search << '\n';
    } else {
        cout << n<<" not found\n";
    }
}

Output:

Output

Enter the element which you want to search: b
b found and the value is b

In the previous example, the find method is utilized to locate the element based on the value provided by the user.

Example 4

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main () {
   multiset<int> mymultiset;
   multiset<int>::iterator it;

   for (int i = 1; i <= 10; i++) mymultiset.insert(i*10);    
   it = mymultiset.find(40);
   mymultiset.erase (it);
   mymultiset.erase (mymultiset.find(60));

   cout << "mymultiset contains:";
   for (it = mymultiset.begin(); it!=mymultiset.end(); ++it)
      cout << ' ' << *it;
   cout << '\n';

   return 0;
}

Output:

Output

mymultiset contains: 10 20 30 50 70 80 90 100

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience