Skip to content
Related Articles

Related Articles

Improve Article

How does generic find() function works in C++ STL?

  • Difficulty Level : Easy
  • Last Updated : 01 Mar, 2021

find(): The find() function is used to search the element in the given range and every STL container has the functionality to search the element using find() function. The generic find function works on every data type.

Return Type:

  • It returns an iterator to the first element in the range [first, last) that equals to the given key
  • If no such element is found, the function returns the iterator to the last element.

Approach:

  • Vector of different data types like int, string, etc and one key element has been taken. 
  • Based on the key element search function is called.
  • The working mechanism of the search function is written using a template.
  • The function searches the element from the start to the end of the vector, based on the key element. If the value doesn’t exist, then it will return the end iterator.
  • If the key element matches with the vector element, then it will return the element along with its position.

Below is the C++ program to illustrate the implementation of generic find() in vector:

C++




// C++ program to illustrate the
// implementation of generic find()
#include <iostream>
#include <vector>
using namespace std;
  
// Two generic templates classes one
// for iterator and other for key
template <class ForwardIterator, class T>
ForwardIterator search(
    ForwardIterator start,
    ForwardIterator end, T key)
{
    while (start != end) {
  
        // If key is present then return
        // the start iterator
        if ((*start) == key) {
            return start;
        }
  
        // Increment the iterator
        start++;
    }
  
    // If key is not present then,
    // return end iterator
    return end;
}
  
// Function to illustrate the use
// of generic find()
void inputElements()
{
    // Vector of integer data type
    vector<int> v{ 10, 20, 40, 30, 50 };
  
    // Element to be searched
    int key = 100;
  
    // Stores the address
    auto it = search(v.begin(), v.end(),
                     key);
  
    if (it != v.end()) {
  
        cout << key << " is present"
             << " at position "
             << it - v.begin() + 1
             << endl;
    }
    else {
  
        cout << key
             << " is not present"
             << endl;
    }
  
    cout << endl;
  
    // Vector of string data type
    vector<string> str{ "C++", "Python",
                        "GFG", "Ruby" };
  
    // Element to be searched
    string key2 = "GFG";
  
    // Stores the address
    auto it2 = search(str.begin(), str.end(),
                      key2);
  
    if (it2 != str.end()) {
  
        cout << key2 << " is present "
             << "at position "
             << it2 - str.begin() + 1
             << endl;
    }
    else {
  
        cout << key2 << " is not Present"
             << endl;
    }
}
  
// Driver Code
int main()
{
    // Function Call
    inputElements();
  
    return 0;
}
Output:
100 is not present

GFG is present at position 3
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :