Search code examples
c++linked-listsetgeneric-listgeneric-collections

C++ set<T> What is the most appropriate solution?


I couldn't get the use of Class or Struct using a C++ Set. I searched on the internet and stackoverflow, but i couldn't find a sample. Classically, it seems impossible to find samples other than int and string samples, as a result of my calls. I wish a friend to help.

Thanks to the friends who will respond.

using namespace std;

struct DemoData
{
    int             id;
    string          Pairs;
    double          Price;

};

int main()
{
    DemoData myDemoDara  ;
    myDemoDara.id = 1; myDemoDara.Pairs = "GBPJPY"; myDemoDara.Price = 9.34;
    set<DemoData> setVeri  ;    //**It gives errors during compilation.**

    setVeri.insert(listem);

    return 0; 
}

Solution

  • You will have to provide a custom operator < for your struct DemoData

    struct DemoData
    {
        int             id;
        string          Pairs;
        double          Price;
        bool operator < (const DemoData& other) const {
         return std::tie(   id, Pairs, Price) < 
                std::tie(   other.id, other.Pairs, other.Price) ;
        }
    };