! , ...
std::map std::pair<const Key, T>, / std::pairs ( const)std::map<>::erase(),cache (, std::partition), cache - 1
:
, , . std::set_difference(), , .
, std::map std::set (std::pair Key), Comparator.
- , const std::pair & a const Key &. ! ( , ... Mac OS X 10.10.5), std::set_difference() Comparator ...
, SFINAE std::set_difference():
using Key = int;
using Value = char;
using Pair = std::map<Key,Value>::value_type;
struct Comparator
{
// Maybe use a custom comparator instead of '<' (see std::set documentation)
template<class P, class K> auto operator()( const P &p, const K &k ) -> decltype(p.first < k)
{ return (p.first < k); }
template<class P, class K> auto operator()( const K &k, const P &p ) -> decltype(k < p.first)
{ return (k < p.first); }
};
int main( void )
{
std::map<Key,Value> cache = { {1, 'a'}, {2, 'b'}, {3, 'c'}, {4, 'd'} };
std::set<Key> selected_items = { 2, 4 };
std::map<Key,Value> new_cache;
std::set_difference( cache.begin(), cache.end(),
selected_items.begin(), selected_items.end(),
std::inserter( new_cache, new_cache.end() ),
Comparator() );
cache = std::move( new_cache ); // Don't use new_cache from here on
return 0;
}