The find() function
In C++, the find()
function is used to search for a specific value in a container, such as an array, vector, or list. The find()
function returns an iterator that points to the first occurrence of the value in the container, or the end iterator if the value is not found.
The find() function seeks a specific value in a range of elements. find() is defined in the algorithms library of the Standard Template Library (STL) , thus adding #include <algorithm>
enables use of find()
Here is an example of how to use find()
with a vector:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> my_vector = {1, 2, 3, 4, 5};
int value = 3;
auto result = find(my_vector.begin(), my_vector.end(), value);
if (result != my_vector.end()) {
cout << "Value found at position " << distance(my_vector.begin(), result) << endl;
} else {
cout << "Value not found" << endl;
}
return 0;
}
In this example, we create a vector<int>
object called my_vector
that contains integers from 1 to 5. We then use the find()
function to search for the value 3 in the vector. If the value is found, we print its position in the vector to the console. If the value is not found, we print a message indicating that the value was not found.
find()
can also be used with other container types, such as arrays and lists. Additionally, find()
can be customized for different data types by providing a custom comparison function.
find()
is a commonly used function in C++ programming, especially when working with containers and searching for specific values.
The find_if() function
The find_if() function is a variation of the find() function, defined in the STL algorithms library, that searches a range for an element that satisfies a boolean condition. Adding #include <algorithm>
enables the use of find_if().
The function's notation is find_if(iteratorFirst, iteratorLast, boolFunction)
:
iteratorFirst: An iterator to the range's first element
iteratorLast: An iterator to one element past the range's last element
boolFunction: A function that takes one argument (of the container type) and returns true or false
find_if() returns an iterator to the first element in the range that causes boolFunction to return true. If no element satisfies the condition, find_if() returns iteratorLast. find_if() goes through the range sequentially and passes each element to boolFunction one-by-one.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cctype>
using namespace std;
bool isFormattedNum(string);
int main() {
vector<string> numberStrings;
vector<string>::iterator iter;
// Adding strings
numberStrings.push_back("fifty two point one");
numberStrings.push_back("2.42");
numberStrings.push_back("899.0");
numberStrings.push_back("63.2");
iter = find_if(numberStrings.begin(), numberStrings.end(), isFormattedNum);
if (iter != numberStrings.end()) {
cout << "The first ##.# formatted string is " << *iter << "." << endl;
}
else {
cout << "No ##.# formatted strings found." << endl;
}
return 0;
}
bool isFormattedNum(string num) {
bool isMatch;
isMatch = false;
if (num.size() == 4) {
if (isdigit(num.at(0)) && isdigit(num.at(1)) &&
isdigit(num.at(3)) && num.at(2) == '.') {
isMatch = true;
}
}
return isMatch;
}
This C++ program uses the STL function find_if()
to find the first string in a vector that matches a specific format (two digits, a period, and another digit) as defined by the isFormattedNum()
function.
Here's a step-by-step breakdown of the program:
The
isFormattedNum()
function checks if a string is formatted as two digits, a period, and another digit. It usesisdigit()
to check if a character is a digit and usesat()
to access individual characters in the string by their indices.In the
main()
function, a vector of strings callednumberStrings
is declared and some strings are added to it using thepush_back()
method.find_if()
is used to find the first string in the vector that matches the condition specified byisFormattedNum()
. It takes in two iterators (pointing to the beginning and end of the vector) and the unary function (or Predicate)isFormattedNum()
.find_if()
returns an iterator pointing to the first element in the range[first, last)
for which the applied function or predicate returns true. If no such element is found, the function returnslast
.The program then checks if the returned iterator equals
end()
(which is returned byfind_if()
if no element satisfied the condition). If it doesn't, it means an element satisfying the condition was found, and the program prints this element. Otherwise, it prints "No ##.# formatted strings found."
In the given list of strings, "2.42" is the first string that matches the format and is thus printed by the program.
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark