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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。