#inserting a set in a set of sets using stl

1 messages · Page 1 of 1 (latest)

haughty knot
#

I am learning about c++ stl, and while studying sets, I am stuck at this problem of inserting a set in a set of sets. Here is what my approach looks like. Kindly point out the flaws and redirect me to the correct approach. Thanks
set<set<int> > nestedSet; nestedSet.insert(set<int>({1,3,2}));

smoky ingotBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

dark ingot
haughty knot
#

yes, i mean a nested set. I understand there would possibly be a problem around the definition of the < operator for comparing two sets. But im sure there's some way around it?

dark ingot
#

but why do you want to nest sets?

unborn cove
#

Maths does deal with sets of sets.

dark ingot
#

fair enough yeah just wanted to check it was that and not that they actually just wanted std::vectors

haughty knot
#

@dark ingot any discrete mathematical representation has to be expressible in code as well, so I think it should be possible to implement

dark ingot
#

but anyway set takes an optional type argument which allows you to specify a comparison operator

By default that comparison uses std::less which uses operator<. The problem is you don't really want to be implementing custom operators for a set since 1. its a standard type and you can't necessarily do that in some cases and 2. you might not necessarily want all sets to behave this way since its quite a generic type

instead there are a couple of options

  1. wrap your set inside a class e.g. MathSet and then you know that every MathSet is a set with the specific properties you want. And then you can do all the operators correctly for that class
  2. provide your own comparison type instead of std::less that knows how to compare sets in the way you want it to (e.g. std::set<std::set<int>, SetCompare>)
  3. you can probably specialize std::less but I can't remember if its allowed to do that with standard types
#

I would likely go with the first option. Just because it means you can define custom operations on sets in a nicer way. And change out your implementation inside at a later date if you want to. And do all the nice invariant stuff. But it leads to more boilerplate

#

but if you just want an "it works" solution you probably want a custom comparitor

haughty knot
#

@dark ingot i still dont get the result. there seems to be an issue with the constructor accepting the initializer list as an argument.

rapid glade
#

That is probably a side-effect of set<set<int> > nestedSet; not being valid.

dark ingot
#

I think set is one of those containers that only compile errors once you try to use a specific aspect of it which it doesn't support

#

in that case its the constructor being invalid

#

because it can't insert the elements

haughty knot
#

nestedSet.insert(set<int>({1,3,2})); alongwith defining a custom comparator and initialising the nestedSet as set<set<int>,Cmp> nestedSet;

dark ingot
#

whats the error?

rapid glade
#

Just copy the text, don't send pictures.

#

Even better, send a link to godbolt.org that reproduces the error so you can get a link back with the fixed code.

haughty knot
#

@rapid glade @dark ingot heres the code link https://godbolt.org/z/nh1M98o5K

rapid glade
#

I have no clue what your Cmp with that std::integral_constant was supposed to be.

haughty knot
#

@rapid glade yeah it works. how did you figure out to put the comparator within a struct?

rapid glade
#

Because the second template parameter for std::set is a type, so you can't just put in a value.

#

You'd think the comparator would be a value and not a type, but apparently not.

haughty knot
#

yeah, pretty cool. thanks.

#

!solved

smoky ingotBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity