map value_comp() in C++ STL
Last Updated :
24 Oct, 2018
The std::map::value_comp() is a function in C++ STL. It returns a function object that compares objects of type std::map::value.
Syntax:
value_compare value_comp() const
Parameters: It does not accept any parameters.
Returns: This method returns a function object that compares objects of type std::map::value.
Time Complexity: O(1)
Below examples illustrate the map::value_comp() method:
Example 1:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, int> m = {
{ 'a', 1 },
{ 'b', 2 },
{ 'c', 3 },
{ 'd', 4 },
{ 'e', 5 },
};
auto last = *m.rbegin();
auto i = m.begin();
cout << "Map contains "
<< "following elements"
<< endl;
do {
cout << i->first
<< " = "
<< i->second
<< endl;
} while (m.value_comp()(*i++, last));
return 0;
}
|
Output:
Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5
Example 2:
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<char, char> m = {
{ 'a', 'A' },
{ 'b', 'B' },
{ 'c', 'C' },
{ 'd', 'D' },
{ 'e', 'E' },
};
auto last = *m.rbegin();
auto i = m.begin();
cout << "Map contains "
<< "following elements"
<< endl;
do {
cout << i->first
<< " = "
<< i->second
<< endl;
} while (m.value_comp()(*i++, last));
return 0;
}
|
Output:
Map contains following elements
a = A
b = B
c = C
d = D
e = E
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...