{
if (_c1.m_yPosition < _c2.m_yPosition)
return true;
else if (_c1.m_yPosition == _c2.m_yPosition)
return _c1.m_xPosition < _c2.m_xPosition;
Else
return true;
}
};
- :
std::map< ChessCoordinate, Figure *, ChessCoordinateComparatorXY > chessDesk;
// ^ ^ ^
//
, . , , < :
template <
typename Key, //
typename Value, //
typename Compare = std::less< Key> // , <
>
class map { /* */ };
std::map , -. , , , . std::map, , - . - key_comp(), , - .
-. std::map . - . - .
- std::function :
typedef std::map<
ChessCoordinate,
Figure *,
std::function< bool (ChessCoordinate, ChessCoordinate) >
> ChessDesk;
-, , -. - std::function:
ChessDesk deskXY(
[] (ChessCoordinate c1, ChessCoordinate c2) -> bool
{
//... ...
}
);
, , std::function , . , std::function (inline) .
, . :
|
|
template < typename Key, typename Value, typename Compare >
std::map< Key, Value, Compare >
make_map (const Compare & c)
{
return std::map< Key, Value, Compare >(c);
}
-:
auto chessDesk = make_map < ChessCoordinate, Figure * >(
[] (ChessCoordinate c1, ChessCoordinate c2) -> bool
{
//... ...
}
);
, make_map .
decltype:
auto comparator = [] (ChessCoordinate c1, ChessCoordinate c2) -> bool
{
//... ...
}
std::map<
ChessCoordinate,
Figure *,
decltype (comparator)
> chessDesk(comparator);
, .
Std::unordered_map
std::unordered_map std::map, , . , , , , , - std::map.
#include <unordered_map>
std::unordered_map< std::string, double > countriesSquare;
^ ^
: std::unordered_map , -. :
● , ( );
● - .
std::unordered_map , , std::map std::unordered_map ( ). , std::unordered_map .
- , std::unordered_map lower_bound, upper_bound equal_range.
, , - , (buckets):
● begin/end , , ;
● bucket_count() ;
● bucket_size(int) , ;
● bucket(K) , ;
● load_factor() float;
● max_load_factor() float, - ;
● max_load_factor(float) ;
|
|
● rehash(int) - , , ;
● reserve(int), , .
std::unordered_map . , , .
std::map, std::unordered_map < . :
● , - size_t;
● (==) .
, std::string , - std::hash. std::equal_to:
template <
typename Key, //
typename Value, //
typename Hash = std::hash< Key > //
typename KeyEq = std::equal_to< Key > // ==
>
class unordered_map { /* */ };
std::unordered_map, . :
struct ChessCoordinateHasher
{
size_t operator () (ChessCoordinate c) const
{
static std::hash< short > s_shortHasher;
return s_shortHasher(cc.m_x) << 4 + s_shortHasher(cc.m_y);
}
};
std::hash< short >. - , . 4 , 1..8, 8 4 . , - ( , ).
, :
bool operator == (ChessCoordinate cc1, ChessCoordinate cc2)
{
return cc1.m_x == cc2.m_x && cc1.m_y == cc2.m_y;
}
- :
std::unordered_map< ChessCoordinate, Figure *, ChessCoordinateHasher > desk;
// ^ ^ ^
//
- std::unordered_map , std::map.