set upper_bound() in C++

upper_bound(): This is a build-in function in STL. This function finds the end of a 

subsequence matching is given key.  

This function returns an iterator pointing to the first element greater than key, 

or end().

Parameters: One Parameter is required for this function.

__x – Key to being located. 

Syntax:

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

For Example:

st = {1,2,6,8}

st.upper_bound(4) = > It returns 6.

Approach

C++

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

int main()
{
    set<intst;
    st.insert({6218});

    set<int>::iterator it = st.upper_bound(4);

    cout << *it << "\n";

    return 0;
}


No comments:

Post a Comment