std::map::value_comp()是C++ STL中的函数。它返回一个函数对象,该对象比较std::map::value类型的对象。
用法:
value_compare value_comp() const
参数:它不接受任何参数。
返回值:此方法返回一个函数对象,该对象比较std::map::value类型的对象。
时间复杂度:O(1)
以下示例说明了map::value_comp()方法:
示例1:
// C++ program to illustrate
// map::value_comp() method
#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;
}
输出:
Map contains following elements a = 1 b = 2 c = 3 d = 4 e = 5
示例2:
// C++ Program to illustrate
// map::rbegin() method
#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;
}
输出:
Map contains following elements a = A b = B c = C d = D e = E
相关用法
注:本文由纯净天空筛选整理自lakshita大神的英文原创作品 map value_comp() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。