×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

std::find() with examples in C++

In this article, we are going to see how to search the position of an element within a range using STL function find()?
Submitted by Radib Kar, on July 17, 2020

find() as a STL function

find() is an STL function that comes under the <algorithm> header file which returns an iterator to the first occurrence of the searching element within a range.

Syntax

InputIterator find(
    InputIterator first, 
    InputIterator last, 
    const T& val);

Parameter(s)

  • InputIterator first - iterator to start of the searching range
  • InputIterator last - iterator to end of the searching range
  • const T& val - value to be searched of datatype T

What is InputIterator?
Iterator to first position of the range where we find the searching element. If searching element is not found it returns iterator to the end

Return value

Return type: bool

Using the above syntax the elements in the corresponding ranges are searched whether the searching element is found.

Time complexity: Linear time, O(n)

binary_search() Vs. find() functions

Difference between binary_search() and find() functions

  • std::binary_search() function returns Boolean telling whether it finds or not. It doesn't return the position. But, std::find() searches the position too. It returns an iterator to the first position.
  • std::binary_search() searches in O(logn) time whether std::find() searches in linear time.

Example 1

When the searched element is found and have only one instance in the searching range

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr{ 1, 2, 3, 8, 4, 3 };

    int searching_element = 8;
    vector<int>::iterator it;
    //starting iterator of range= arr.begin()
    //end iterator of range =arr.end()

    it = find(arr.begin(), arr.end(), searching_element);
    if (it != arr.end())
        cout << searching_element << " is at position: " << it - arr.begin() << endl;
    else
        cout << searching_element << "not found";

    return 0;
}

Output

8 is at position: 3

In the above program, we have checked the searching element and we found it at 3rd index(0-indexing)

Example 2

When the searched element is found and have more than one instance in the searching range

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr{ 1, 2, 3, 8, 4, 3 };

    int searching_element = 3;
    vector<int>::iterator it;
    //starting iterator of range= arr.begin()
    //end iterator of range =arr.end()

    it = find(arr.begin(), arr.end(), searching_element);
    if (it != arr.end())
        cout << searching_element << " is at position: " << it - arr.begin() << endl;
    else
        cout << searching_element << "not found";

    return 0;
}

Output

3 is at position: 2 

In the above case, we are searching 3 in the array which has two instances one at position index 2 and the other is at position 5. Since std::find() stops searching whenever it finds the searching element thus it returns index 2 (Check how we found the position from the iterator it returned).

Example 3

When the searched element is not found in the searching range

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr{ 1, 2, 3, 8, 4, 3 };

    int searching_element = 7;
    vector<int>::iterator it;
    //starting iterator of range= arr.begin()
    //end iterator of range =arr.end()
 
    it = find(arr.begin(), arr.end(), searching_element);
    if (it != arr.end())
        cout << searching_element << " is at position: " << it - arr.begin() << endl;
    else
        cout << searching_element << " not found";

    return 0;
}

Output

7 not found

In the above case, the searching element is not present and that's why it returned arr.end() which means iterator to the end of the range.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.