-1

I've a some class where I'm declaring a set like this:

std::set<UPFIR::RetentionSignal*> _retSignalSet; 

I'm trying to use std::less compare function on it. I tried something like this:

std::set<UPFIR::RetentionSignal*, std::less<UPFIR::RetentionSignal*>> _retSignalSet; 

The feedback I'm getting is "adding std::less wont make it determinism. You have to compare the name", Can somebody explain me how can this be accomplished as I haven't worked with std::less before?

Thanks

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 1
    The comment "won't make it deterministic" is wrong, because `std::less` is defined to provide a total order on pointers. However, the order may not be the one required to fulfill your task. If the order must by name, then `std::less` does not help. You must provide a custom comparator that compares the name. – j6t Dec 06 '21 at 07:46
  • please provide a [mre]. What is the code supposed to do? What is `RetentionSignal`? – Alan Birtles Dec 06 '21 at 08:01
  • 1
    @masoud the downvote is likely because OP received "you have to compare the name" request but his question does neither give code where "the name" participates nor tell anything about nature of "the name". As such the question is unanswerable. – Öö Tiib Dec 06 '21 at 08:21
  • @AlanBirtles Sorry for the incomplete info, what I'm trying to do here is save the pointers in the set and extract the information by traversing it afterwards to dump it in a file. The way I'm doing this is: for (auto it: _retSignalSet) { std::string s1,s2; //populate strings } mprintf("%s%s", s1,s2); – Danger69420 Dec 06 '21 at 08:31
  • 2
    @Danger69420: You should put the code in the question. – masoud Dec 06 '21 at 09:51
  • @Danger69420 Please read [ask] with a [mcve]. We need a complete example to stop _"but what about ...?"_ chains in the comments. Please add all additional information to the question as code in the comments is not formatted. – Richard Critten Dec 06 '21 at 10:20
  • @j6t -- that comment should be expanded a bit into an answer. The question is clear enough to warrant an answer. – Pete Becker Dec 06 '21 at 14:04

1 Answers1

0

If the requirement is that the set should be sorted by name, then std::less does not help. You must provide a custom comparator that compares the name. For example (just an untested sketch):

struct LessByName {
    bool operator<(UPFIR::RetentionSignal* a, UPFIR::RetentionSignal* b)
    {
        return a->name < b->name;
    }
};

and then use it like

std::set<UPFIR::RetentionSignal*, LessByName> _retSignalSet;

BTW, the comment "std::less won't make it deterministic" is wrong, because std::less is defined to provide a total order on pointers. The order you get may just not be the one required to fulfill your task.

j6t
  • 9,150
  • 1
  • 15
  • 35
  • Hi @j6t this works perfectly for a set defined in main but how can this be done if the set is declared inside a class like: class foo { private: std::set _retSignalSet; } – Danger69420 Dec 07 '21 at 05:52
  • @Danger69420 Where is the problem? Does it help if you move the `struct LessByName` definition into the class right before the `std::set... _retSignalSet` definition? – j6t Dec 07 '21 at 07:09
  • Yes moving the struct inside the class works perfectly fine, thanks. – Danger69420 Dec 09 '21 at 03:56