std::map::key_comp()是C++ STL中的内置函数,它返回容器使用的比较对象的副本。默认情况下,这是一个less对象,其返回值与运算符‘
用法:
key_compare map_name.key_comp();
参数:该函数不接受任何参数。
返回值:该函数返回容器使用的比较对象的副本。
以下示例说明了map::key_comp()方法:
示例1:
// C++ program to illustrate the
// map::key_comp() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a map named m;
map<char, int> m;
map<char, int>::key_compare
comp
= m.key_comp();
// Inserting elements into map
m['a'] = 10;
m['b'] = 20;
m['c'] = 30;
m['d'] = 40;
cout << "Map has the elements\n";
// Store key value of last element
char l = m.rbegin()->first;
// initializing the iterator
map<char, int>::iterator it = m.begin();
// printing elements of all map
do {
cout << it->first
<< " => "
<< it->second
<< '\n';
} while (comp((*it++).first, l));
return 0;
}
输出:
Map has the elements a => 10 b => 20 c => 30 d => 40
示例2:
// C++ program to illustrate the
// map::key_comp() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a map named m;
map<char, int> m;
map<char, int>::key_compare
comp
= m.key_comp();
// Inserting elements into map
m['a'] = 1;
m['b'] = 2;
m['c'] = 3;
m['d'] = 4;
cout << "Map has the elements\n";
// Store key value of last element
char l = m.rbegin()->first;
// initializing the iterator
map<char, int>::iterator it = m.begin();
// printing elements of all map
do {
cout << it->first
<< " => "
<< it->second
<< '\n';
} while (comp((*it++).first, l));
return 0;
}
输出:
Map has the elements a => 1 b => 2 c => 3 d => 4
相关用法
注:本文由纯净天空筛选整理自ankit15697大神的英文原创作品 map key_comp() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。