How?I found a lot of examples, but the example with template of the parameter could not be found. I somehow did not work comparator.
std::map<K, CacheEntry<T>*, Comparator<K, CacheEntry<T>>> timeMap_;
template<typename T1, typename T2>
struct Comparator
{
typedef std::pair<T1, T2> type;
bool operator()(const type& l, const type& r) const
{
auto nowTime = std::chrono::system_clock::now();
auto timeL = nowTime - l.second->creationTime();
auto timeR = nowTime - r.second->creationTime();
return (std::chrono::duration_cast<std::chrono::microseconds>(timeL).count() > std::chrono::duration_cast<std::chrono::microseconds>(timeR).count());
}
};
Error:
Error 1 error C2664: 'bool diadoc::cache::Comparator *>::operator ()(const std::pair &,const std::pair &) const' : cannot convert argument 1 from 'const std::wstring' to 'const std::pair &' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 521 1 DiadocClient
I try use:
template<typename T1, class T2>
But it's too not working. Sort by the second parameter map.
Kin your example? Anyway, the comparator object passed tostd::mapmust accept two objects of typeKas arguments. Can't you just switch the template arguments tomap? - Angew is no longer proud of SOstd::mapsorts by key, and uses the fact that its contents is sorted to provide logarithmic-time uniqueness checks and access. Can you specify your requirements clearly: What are your uniqueness criteria, sorting criteria, access/modification time complexity criteria? It might turn out you need a more complex container such asboost::multi_index. - Angew is no longer proud of SO