Skip to main content

std::map value_comp() method

// Const version only
value_compare value_comp() const;

Returns a function object that compares objects of type value_type (key-value pairs) by using key_comp() to compare the first components of the pairs.

Parameters

(none)

Return value

The value comparision function object.

Complexity

Constant - O(1).

Example

Main.cpp
#include <map>
#include <iterator>
#include <iostream>
#include <utility>
#include <initializer_list>

void print(auto const comment, auto const& container)
{
auto size = std::size(container);
std::cout << comment << "{ ";
for (auto const& [key, value]: container)
std::cout << '{' << key << ',' << value << (--size ? "}, " : "} ");
std::cout << "}\n";
}

int main()
{
std::map<int, int> x { {1,1}, {2,2}, {3,3} }, y, z;
const auto w = { std::pair<const int, int>{4,4}, {5,5}, {6,6}, {7,7} };

std::cout << "Initially:\n";
print("x = ", x);
print("y = ", y);
print("z = ", z);

std::cout << "Copy assignment copies data from x to y:\n";
y = x;
print("x = ", x);
print("y = ", y);

std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
z = std::move(x);
print("x = ", x);
print("z = ", z);

std::cout << "Assignment of initializer_list w to z:\n";
z = w;
print("w = ", w);
print("z = ", z);
}
Output
Initially:
x = { {1,1}, {2,2}, {3,3} }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { {1,1}, {2,2}, {3,3} }
y = { {1,1}, {2,2}, {3,3} }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { {1,1}, {2,2}, {3,3} }
Assignment of initializer_list w to z:
w = { {4,4}, {5,5}, {6,6}, {7,7} }
z = { {4,4}, {5,5}, {6,6}, {7,7} }
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::map value_comp() method

// Const version only
value_compare value_comp() const;

Returns a function object that compares objects of type value_type (key-value pairs) by using key_comp() to compare the first components of the pairs.

Parameters

(none)

Return value

The value comparision function object.

Complexity

Constant - O(1).

Example

Main.cpp
#include <map>
#include <iterator>
#include <iostream>
#include <utility>
#include <initializer_list>

void print(auto const comment, auto const& container)
{
auto size = std::size(container);
std::cout << comment << "{ ";
for (auto const& [key, value]: container)
std::cout << '{' << key << ',' << value << (--size ? "}, " : "} ");
std::cout << "}\n";
}

int main()
{
std::map<int, int> x { {1,1}, {2,2}, {3,3} }, y, z;
const auto w = { std::pair<const int, int>{4,4}, {5,5}, {6,6}, {7,7} };

std::cout << "Initially:\n";
print("x = ", x);
print("y = ", y);
print("z = ", z);

std::cout << "Copy assignment copies data from x to y:\n";
y = x;
print("x = ", x);
print("y = ", y);

std::cout << "Move assignment moves data from x to z, modifying both x and z:\n";
z = std::move(x);
print("x = ", x);
print("z = ", z);

std::cout << "Assignment of initializer_list w to z:\n";
z = w;
print("w = ", w);
print("z = ", z);
}
Output
Initially:
x = { {1,1}, {2,2}, {3,3} }
y = { }
z = { }
Copy assignment copies data from x to y:
x = { {1,1}, {2,2}, {3,3} }
y = { {1,1}, {2,2}, {3,3} }
Move assignment moves data from x to z, modifying both x and z:
x = { }
z = { {1,1}, {2,2}, {3,3} }
Assignment of initializer_list w to z:
w = { {4,4}, {5,5}, {6,6}, {7,7} }
z = { {4,4}, {5,5}, {6,6}, {7,7} }
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.