How to have a set of structs in C++

前端 未结 7 502
渐次进展
渐次进展 2021-02-01 21:01

I have a struct which has a unique key. I want to insert instances of these structs into a set. I know that to do this the < operator has to be overloaded so that set can mak

7条回答
  •  灰色年华
    2021-02-01 21:42

    The problem isn't in your set; it's in your test object. You're using Java style there. In C++, we just write:

    set bar;
    
    int main()
    {
        foo test; // Local variable, goes out of scope at }
        test.key = 0;
        bar.insert(test); // Insert _a copy of test_ in bar.
    }
    

提交回复
热议问题