map::key_comp()是C++中STL中的函数,它返回比较对象的副本,该对象由比较键的容器使用。
用法:
map.key_comp()
返回值:此方法返回比较键的容器使用的比较对象。
以下示例说明了key_comp()方法的用法方式:
例:
// C++ program to demonstrate map::key_comp().
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Declare the map
map<char, int> mymap;
// Compare the key.
map<char, int>::key_compare
mycomp
= mymap.key_comp();
// Populate the map
mymap['x'] = 50;
mymap['y'] = 100;
mymap['z'] = 150;
// Print the map
cout << "mymap contain:\n";
char highest = mymap.rbegin()->first;
// key value of last element
map<char, int>::iterator
it
= mymap.begin();
do {
cout << it->first
<< " => " << it->second
<< "\n";
} while (mycomp((*it++).first, highest));
cout << "\n";
return 0;
}
输出:
mymap contain: x => 50 y => 100 z => 150
示例2:
// C++ program to demonstrate map::key_comp().
#include <iostream>
#include <map>
using namespace std;
int main()
{
// Declare the map
map<char, int> mymap;
// Compare the key.
map<char, int>::key_compare
mycomp
= mymap.key_comp();
mymap['a'] = 100;
mymap['b'] = 200;
mymap['c'] = 300;
cout << "mymap contain:\n";
char highest = mymap.rbegin()->first;
// key value of last element
map<char, int>::iterator
it
= mymap.begin();
do {
cout << it->first
<< " => "
<< it->second
<< '\n';
} while (mycomp((*it++).first, highest));
cout << '\n';
return 0;
}
输出:
mymap contain: a => 100 b => 200 c => 300
相关用法
- C++ div()用法及代码示例
- C++ fma()用法及代码示例
- C++ log()用法及代码示例
- PHP image_type_to_mime_type()用法及代码示例
- PHP image_type_to_extension()用法及代码示例
- C++ map rend()用法及代码示例
注:本文由纯净天空筛选整理自Samdare B大神的英文原创作品 map key_comp() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。