- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <multiset >
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ multiset Library - value_comp() Function
Description
The multiset::value_comp() function in C++ STL returns a copy of the comparison object which is used by the multiset to compare values. The returned comparison object defines the ordering of the elements in the multiset.
In multiset, since keys and values are the same, the returned comparison object of value_comp() is same as key_comp().
Syntax
Following is the syntax of multiset::value_comp() function −
value_compare multiset_name.value_comp() const;
Parameters
The value_comp() function does not accept any parameters.
Return value
The value_comp() function returns a copy of the comparison object used by the multiset. By default, it uses the less than(<) operator for comparison.
Exceptions
If an exception is thrown, the container remains unchanged.
Time complexity
The time complexity of multiset::value_comp() function is O(1) (constant time).
Examples of multiset::value_comp() Function
The following examples demonstrate the usage of multiset::value_comp() function in multiset:
Using value_comp() to Compare Elements
Below is an example to compare two integers (val1 and val2) in multiset using the comparison object returned by multiset::value_comp() function. The (comp(val1, val2)) checks val1 < val2.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> numbers = {15, 25, 35, 45, 55};
cout << "Multiset elements:";
for (const auto &num : numbers)
cout << " " << num;
cout << endl;
auto comp = numbers.value_comp();
int val1 = 30, val2 = 50;
cout << "Comparing " << val1 << " with " << val2 << ":\n";
if (comp(val1, val2))
cout << val1 << " is less than " << val2 << endl;
else
cout << val1 << " is not less than " << val2 << endl;
cout << "\nComparing " << val2 << " with " << val1 << ":\n";
if (comp(val2, val1))
cout << val2 << " is less than " << val1 << endl;
else
cout << val2 << " is not less than " << val1 << endl;
return 0;
}
The output of the above code is given below −
Multiset elements: 15 25 35 45 55 Comparing 30 with 50: 30 is less than 50 Comparing 50 with 30: 50 is not less than 30
Printing Values Less Than or Greater Than a Specific Value
You can use the comparison object returned by value_comp() to print all values in the multiset that are less than or greater than a specific value. Here, we have printed all the values that are less than and greater than the limit, which is 40.
#include <iostream>
#include <set>
using namespace std;
int main() {
multiset<int> nums = {10, 40, 20, 30, 40, 50, 90, 35};
cout << "Multiset values:";
for (const auto &num : nums)
cout << " " << num;
cout << endl;
auto comp = nums.value_comp();
int limit = 40;
cout << "Values less than " << limit << " are: ";
for (auto it = nums.begin(); it != nums.end(); ++it)
{
if (comp(*it, limit))
cout << *it << " ";
else
break;
}
cout << endl;
cout << "Values greater than " << limit << " are: ";
for (auto it = nums.begin(); it != nums.end(); ++it)
{
if (comp(limit, *it))
cout << *it << " ";
}
cout << endl;
return 0;
}
The output of the above code is given below −
Multiset values: 10 20 30 35 40 40 50 90 Values less than 40 are: 10 20 30 35 Values greater than 40 are: 50 90
Using value_comp() to Compare Strings
In this example, we have compared two string elements of multiset using the value_comp() function −
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
multiset<string> names = {"Aryan", "Bob", "Viper", "David"};
cout << "Strings in Multiset: ";
for (const auto &name : names)
cout << " " << name;
cout << endl;
auto comp = names.value_comp();
cout << "Checking if 'Viper' comes before 'Aryan': ";
if (comp("Viper", "Aryan"))
cout << "Yes, Viper comes before Aryan" << endl;
else
cout << "No, Aryan comes before Viper" << endl;
return 0;
}
The output of the above code is given below −
Strings in Multiset: Aryan Bob David Viper Checking if 'Viper' comes before 'Aryan': No, Aryan comes before Viper