Set find() function in C++ STL

This tutorial will discuss about the set find() function in C++ stl.

Table Of Contents

In the C++ Standard Template Library (STL), the set is a container that stores unique elements in a specific order. The find() member function of the set container allows for efficient searches for elements within the set.

Syntax of set::find() function

find(const value_type& value) const

This find() function searches the set for an element with the given value. If the element is found, it returns an iterator pointing to that element; otherwise, it returns an iterator pointing to the position just after the last element in the set (often represented by set::end()).

Parameters of set::find()

- `value`: The value of the element that we want to search in the set. The type of this parameter is `value_type`, corresponding to the data type of the elements stored in the set.

Return Value of set::find()

- If an element with the given value is found, the function returns an iterator pointing to that element.
- If no such element is found, the function returns an iterator pointing to the position just after the last element in the set (`set::end()`).

Example of set::find()

Let’s see the complete example,

#include <iostream>
#include <set>

int main()
{
    std::set<int> numbers = {21, 43, 86, 32, 90};

    // Search for the value 86 in set
    std::set<int>::iterator it = numbers.find(86);

    if (it != numbers.end())
    {
        std::cout << "Element " << *it << " found in the set.n";
    }
    else
    {
        std::cout << "Element not found in the set.n";
    }
    // Output: Element 43 found in the set.
    return 0;
}

Output

Element 86 found in the set.

Scenarios for Exceptions and Undefined Behavior

  1. Iterator Invalidation: After certain operations (like calling functions set::insert(), set::erase), some iterators might get invalidated. Using invalidated iterators in combination with set::find() or any other function can lead to undefined behavior.

  2. Usage of the Returned Iterator: If the find() function doesn’t locate the element in the set, it returns an iterator pointing to set::end(). Dereferencing this iterator results in undefined behavior.

  3. Thread Safety: If the set is being accessed from multiple threads, and at least one of them modifies the set while others are performing search operations using find(), it might lead to race conditions or undefined behavior. In such scenarios, it’s vital to use synchronization primitives or other concurrency control mechanisms.

Complexity of set::find() function

The find() function in the set container offers a quick and efficient way to search for elements. Given that sets in C++ are typically implemented as balanced binary search trees (like Red-Black Trees), the find() operation is logarithmic in complexity.

Summary

Today, we learned about set find() function in C++ stl.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top