.find()

Anonymous contributor's avatar
Anonymous contributor
Published Mar 8, 2025
Contribute to Docs

The .find() method in C++ searches for a specific element in a std::set. If the element is found, .find() returns an iterator pointing to the element; otherwise, it returns an iterator to set.end(). This method is part of the Standard Template Library (STL).

It allows efficient lookups in std::set, which is typically implemented as a balanced binary search tree. This ensures an average time complexity of O(log n) for lookups.

Syntax

std::set<data_type>::iterator it = set_name.find(value);

Parameters:

  • set_name: The name of the std::set being searched.
  • value: The element to search for in the set.

Return Value:

  • If the element is found, it returns an iterator pointing to the element.
  • If the element is not found, it returns an iterator to set.end().

Examples

Finding an Element in a Set

The following example demonstrates how to search for an element in a std::set and determine whether it exists.

#include <iostream>
#include <set>
int main() {
std::set<int> numbers = {10, 20, 30, 40, 50};
int searchValue = 30;
auto it = numbers.find(searchValue);
if (it != numbers.end()) {
std::cout << "Element found: " << *it << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
return 0;
}

The output will be:

Element found: 30

Checking if a Product Exists in Inventory

This example shows how .find() helps determine whether a product ID exists in the inventory.

#include <iostream>
#include <set>
int main() {
std::set<int> productIDs = {101, 202, 303, 404, 505};
int searchID = 303;
std::set<int>::iterator it = productIDs.find(searchID);
if (it != productIDs.end()) {
std::cout << "Product ID " << searchID << " is available in inventory."
<< std::endl;
} else {
std::cout << "Product ID " << searchID << " is not in inventory."
<< std::endl;
}
return 0;
}

The output will be:

Product ID 303 is available in inventory.

Searching for Employees in a Company Directory

This example uses .find() to search for an employee’s name in a directory, ensuring efficient access to records.

#include <iostream>
#include <set>
#include <string>
struct CompareIgnoreCase {
bool operator()(const std::string &a, const std::string &b) const {
return a < b;
}
};
int main() {
std::set<std::string, CompareIgnoreCase> names = {"Alice", "Bob", "Charlie"};
std::string searchName = "Bob";
auto it = names.find(searchName);
if (it != names.end()) {
std::cout << "Name found: " << *it << std::endl;
} else {
std::cout << "Name not found." << std::endl;
}
return 0;
}

The output will be:

Name found: Bob

Codebyte Example

This example demonstrates a simple use of .find() in a C++ STL set. It checks for the presence of a specific integer in a set and prints whether the element was found or not.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C++ on Codecademy