set find() in C++

find(): This function is a build-in function in STL. This function tries to locate an element in a set. This function returns an iterator pointing to the sought-after element, or end() if not found.

This function takes a key and tries to locate the element with which the key matches. 

1. If successful the function returns an iterator pointing to the sought-after element. 

2. If unsuccessful it returns the past-the-end ( end() ) iterator.

Parameters: 

__x – Element to be located.

Syntax:

set<data_type>::iterator it = st.find(__x)

For Example:

st = {1,2,3,4}

st.find(3) = > It points to 3.

Approach

C++

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

int main()
{
    set<intst;
    st.insert(4);
    st.insert(2);
    st.insert(3);
    st.insert(1);

    set<int>::iterator it = st.find(3);
    cout << *it << "\n";

    return 0;
}


No comments:

Post a Comment