#inserting a set in a set of sets using stl
1 messages · Page 1 of 1 (latest)
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.
what do you mean putting a set inside a set
Do you understand what a set is?
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?
but why do you want to nest sets?
Maths does deal with sets of sets.
fair enough yeah just wanted to check it was that and not that they actually just wanted std::vectors
@dark ingot any discrete mathematical representation has to be expressible in code as well, so I think it should be possible to implement
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
- wrap your set inside a class e.g.
MathSetand then you know that everyMathSetis a set with the specific properties you want. And then you can do all the operators correctly for that class - provide your own comparison type instead of
std::lessthat knows how to comparesets in the way you want it to (e.g.std::set<std::set<int>, SetCompare>) - you can probably specialize
std::lessbut 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
@dark ingot i still dont get the result. there seems to be an issue with the constructor accepting the initializer list as an argument.
That is probably a side-effect of set<set<int> > nestedSet; not being valid.
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
what did you try?
nestedSet.insert(set<int>({1,3,2})); alongwith defining a custom comparator and initialising the nestedSet as set<set<int>,Cmp> nestedSet;
whats the error?
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.
@rapid glade @dark ingot heres the code link https://godbolt.org/z/nh1M98o5K
This is one way to do it: https://godbolt.org/z/xxhs4z3h5.
I have no clue what your Cmp with that std::integral_constant was supposed to be.
@rapid glade yeah it works. how did you figure out to put the comparator within a struct?
Because the second template parameter for std::set is a type, so you can't just put in a value.
https://godbolt.org/z/hKqM9WWxo is an alternative, but it requires C++20.
You'd think the comparator would be a value and not a type, but apparently not.
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