set equal_range() in C++

equal_range(): This is a build-in function in STL. This function is used to finds a subsequence 

matching the given key.

This function returns pair of iterators that possibly points to the subsequence matching given key.

Parameters: One parameter is required for this function.

 __x – Key to being located. Returns: 

Syntax:

pair<set<data_type>::iterator,set<data_type>::iterator> p = it.equal_range(__x)

For Example:

st = {1,2,3,4}

pair<set<int>::iterator,set<int>::iterator> p = st.equal_range(3) => It returns {3,4}

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);

    pair<set<int>::iteratorset<int>::iteratorp = 
st.equal_range(3);

    cout << *p.first << " " << *p.second << "\n";

    return 0;
}

No comments:

Post a Comment