std::set custom key class



  • Hallo!
    Ich möchte eine eigene Klasse als Key für Sets und Maps verwenden.
    Die Klasse sieht z.b. so aus:

    class foo 
    {
     public:
      foo(string a, string b) {this->a = a; this->b = b};
      string a;
      string b;
    
      bool operator==(const foo& ref) const { a.compare(ref.a) == 0; };
      bool operator<(const foo& ref) const ( a.compare(ref.x) < 0;};
    }
    

    und jetzt ein set:

    set<foo> s;
    s.insert(foo("x","y"));
    

    und jetzt möchte ich nach einem Key suchen:

    // Version 1
    s.find(foo("x","y"));
    
    // Version 2
    s.find(foo("x",""));
    

    Also Version 1 findet den entsprechenden Eintrag im Set, Version 2 jedoch nicht. Wie muss man den Code umändern, damit auch Version 2 den entsprechenden Eintrag findet?

    Danke!



  • ok, ich habe jetzt eine Lösung gefunden:

    foo: operator ();
    
    std::find(...);
    

    Wäre aber trotzdem interessant, ob es eine andere Möglichkeit gibt.



  • Poste mal Code der sich auch compilieren lässt, vielleicht hilft dir dann jemand.


Anmelden zum Antworten