Open In App
Related Articles

set value_comp() function in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
The set::value_comp() is an inbuilt function in cpp that returns a copy of the comparison object used by the container. This object determines the order of the elements in the container. It is a function pointer or a function object that takes two arguments of the same type as the container elements, and returns true if the first argument is considered to go before the second in the strict weak ordering it defines, and false otherwise. Two elements of a set are considered equivalent if value_comp returns false reflexively (i.e., no matter the order in which the elements are passed as arguments). Syntax:
value_compare set_name.value_comp() 
Parameters: This function does not accept any parameter. Return value: The function returns a copy of the comparison object used by the container. Below program illustrates the above function.
// CPP program to demonstrate the
// set::value_comp()
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // initialising set a
    set<int> a;
  
    set<int>::value_compare comp = a.value_comp();
  
    // inserting elements to set a
    for (int i = 0; i <= 10; i++)
        a.insert(i);
  
    cout << "Set a has the numbers ";
  
    // start stores value of the last element of set a
    int start = *a.rbegin();
  
    // initialising iterator it
    set<int>::iterator it = a.begin();
  
    // Function that prints all the numbers in set
    do {
        std::cout << *it << " ";
    } while (comp(*(++it), start));
  
    std::cout << '\n';
  
    return 0;
}

                    
Output:
Set a has the numbers 0 1 2 3 4 5 6 7 8 9

Last Updated : 25 Jun, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads