map choose `std::greater` or `std::less` at runtime

  • A+
Category:Languages

This line:

std::map<long int, long int, std::greater<long int>> current_book; 

I would like to replace it by the logical equivalent of:

int Side = ... if (Side == 1){     std::map<long int, long int, std::greater<long int>> current_book; } else {     std::map<long int, long int, std::less<long int>> current_book; } 

 


You can use std::function for that:

using mymap = std::map<long,long,std::function<bool(long,long)>>; auto m = Side ? mymap( std::less<long>() ) : mymap( std::greater<long>() ); 

live example

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: