1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <map>
#include <functional>
int main()
{
{
using map_type = std::map< int, std::string > ;
const map_type map { {1,"klm"}, {3,"abcdef"}, {6,"ghijk"}, {5,"klm"}, {4,"kl"}, {2,"klm"} };
// map_type::key_compare compares keys ie. map_type::key_type ie. int
const /* auto */ map_type::key_compare cmpk = map.key_comp() ; // std::less<int>
const /* int */ map_type::key_type ka = 67, kb = 78 ;
std::cout << std::boolalpha << cmpk(ka,kb) << '\n' ; // 67 < 78, true
// map_type::value_compare compares values ie. map_type::value_type
// ie. std::pair< const map_type::key_type, map_type::mapped_type > ;
const /* auto */ map_type::value_compare cmpv = map.value_comp() ;
const /* int */ map_type::value_type va = { 67, "hello" }, vb = { 78, "world" } ;
// cmpv(va,vb) compares va, vb with va.first < vb.first (cmpv ignores va.second, vb.second)
std::cout << std::boolalpha << cmpv(va,vb) << '\n' ; // 67 < 78, true
}
{
using map_type = std::multimap< int, std::string, std::greater<> > ;
const map_type map { {1,"klm"}, {3,"abcdef"}, {1,"ghijk"}, {3,"klm"}, {3,"kl"}, {1,"klm"} };
// map_type::key_compare compares keys ie. map_type::key_type ie. int;
const /* auto */ map_type::key_compare cmpk = map.key_comp() ; // std::greater<>
const /* int */ map_type::key_type ka = 67, kb = 78 ;
std::cout << std::boolalpha << cmpk(ka,kb) << '\n' ; // !(67>78), false
// map_type::value_compare compares values ie. map_type::value_type
// ie. std::pair< const map_type::key_type, map_type::mapped_type > ;
const /* auto */ map_type::value_compare cmpv = map.value_comp() ;
const /* int */ map_type::value_type va = { 67, "8hello" }, vb = { 67, "1world" } ;
// cmpv(va,vb) compares va, vb with va.first > vb.first (cmpv ignores va.second, vb.second)
std::cout << std::boolalpha << cmpv(va,vb) << '\n' ; // !(67>67), false
}
}
|