The following data structure is available:

std::vector< std::set<unsigned> > G; 

and class for comparison:

 struct classcomp{ unsigned *ia; bool operator() (const int& a, const int& b) const { return (ia[a + 1] - ia[a])< (ia[b + 1] - ia[b]); } }; 

To compare this class you need to create an instance of the class:

 classcomp C; C.ia = ia; 

But create then

 std::vector< std::set<unsigned,C> > G; 

It is impossible, because the second parameter requires the name of the class, not its instance. Tell me how to get around this moment.

  • Remove the default constructor from the classcomp, it is useless for a comparator in std :: set. - Abyx
  • And what exactly is the problem? - Qwertiy

1 answer 1

That's right, in the template you need to specify the type:

 std::vector<std::set<unsigned, classcomp>> G; 

When adding items, use a constructor that accepts a comparator.

 G.emplace_back(C); 

In this case, std::set<unsigned, classcomp> cannot use the default constructor; for this G.resize(n); , for example, G.resize(n); cannot be called G.resize(n); , should be used instead

 G.resize(n, std::set<unsigned, classcomp>(C)); 
  • Apparently the question is no longer on the topic: when an element is inserted into G[i] second time, the comparator is just being accessed, an access error occurs. - Eugene
  • @ Eugene why did you minus the answer? Is it correct, or do you require that the type of the comparator class instance be automatically calculated? Then if you have access to C ++ 11 , then use decltype : std::vector<std::set<unsigned, decltype(C)>> G; - StateItPrimitive
  • @StateItPrimitive I never thought to minus the answer, on the contrary I wanted to put a plus. What makes you think that I zaminusoval? - Eugene
  • @ Eugene "Thought it out", I apologize. Could you give me the minimum complete code that generates the previously described access error so that you can be helped faster? If this does not relate directly to the question, then you can place an example of such a code right here , and then give us a reference, or issue a new question. - StateItPrimitive pm
  • @StateItPrimitive is okay) added to the main message - Eugene