
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
multimap::value_comp Function in C++ STL
In this article we will be discussing the working, syntax and examples of multimap::value_comp() function in C++ STL.
What is Multimap in C++ STL?
Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.
What is multimap::value_comp()?
multimap::value_comp() is an inbuilt function in C++ STL which is declared in <map> header file. value_comp() returns a copy of the comparison object, which is used by the multimap container for the comparisons. By default, this object is less than the operator’s object, which works similarly to a less-than operator.
It is a type of function pointer or a function object which do the comparison of the two values of the same type in a particular multimap and returns true if the first element is smaller than the second element in the container, else it returns false.
Syntax
multi_name.value_comp();
Parameter
This function accepts no parameter.
Return value
This function returns a comparison object of the associated multimap container.
Input
multimap<char, int> newmap; newmap(make_pair(‘a’, 1)); newmap(make_pair(‘b’, 2)); newmap(make_pair(‘c’, 3)); multimap<int>::value_compare cmp = myset.value_comp();
Output
1 2 3
Example
#include <iostream> #include <map> using namespace std; int main(){ multimap<int, char> mul; //inserting elements at given key mul.insert(make_pair(0, 'A')); mul.insert(make_pair(1, 'B')); mul.insert(make_pair(2, 'C')); mul.insert(make_pair(3, 'D')); pair<int, char> temp = *mul.rbegin(); multimap<int, char>::iterator it = mul.begin(); cout<<"Elements at given key is : "<<'\n'; do { cout << (*it).first << " = " << (*it).second << '\n'; } while (mul.value_comp()(*it++, temp)); return 0; }
Output
If we run the above code it will generate the following output −
Elements at given key is : 0 = A 1 = B 2 = C 3 = D