开发者

passing more data to std:set Comparison class

开发者 https://www.devze.com 2022-12-20 23:15 出处:网络
I have an std::set with the Compare class which requires additional parameter to compare keys. This variable parameter is determined in run-time and I pack it inside the set\'s keys just to make it ac

I have an std::set with the Compare class which requires additional parameter to compare keys. This variable parameter is determined in run-time and I pack it inside the set's keys just to make it accessible to Compare.

However, the parameter logically belongs to the set rather than the keys so this solution looks awkward and duplicates the same value over all keys.

Is there an elegant way to pass additional parameter to the Compare class? (I first thought about set::value_comp, but it returns compare object by value while I need a referen开发者_运维百科ce).

Thanks.


The definition of std::set is:

template <
      class Key, 
       class Traits=less<Key>, 
       class Allocator=allocator<Key> 
>
class set

So Traits is should be your compare operator, but if would look at constructor, you can see it there again:

explicit set(
   const Traits& _Comp
);

So just pass your instance to constructor. (Note it is done by copying)

0

精彩评论

暂无评论...
验证码 换一张
取 消