0

given the variable of different nested containers.

std::pair<NonTerminal, std::vector<std::vector<Symbol>>> productions;

How can I order the pair-entries ascending from 0 to ..., such that I can maybe make use of an ::iterator - if even possible/necessary (better way of organization?)

I thought of nesting the above in another pair like this:

std::pair<int, std::pair<NonTerminal, std::vector<std::vector<Symbol>>>> productions;

How could I then in ascending order iterate over my internal representation of grammatical productions (where the NonTerminal of the inner std::pair is the right-hand-side of the production, and the std::vector the right-hand-side.)

Thanks !

EDIT:

What I want is give a total order to the productions

std::pair<NonTerminal, std::vector<std::vector<Symbol>>> productions;

and iterate over them in ascending order.

@ jarod42: Here (Ruby - sort array of objects by attribute in descending order) is a good example how to order by any attribute in ascending/descending order

von spotz
  • 875
  • 7
  • 17
  • 4
    I don't understand what you want... Possibly give examples. – Jarod42 Nov 16 '20 at 15:15
  • We have no idea what do you mean by "*order to the productions*". – Fureeish Nov 16 '20 at 15:25
  • Order by *what*? – Marshall Clow Nov 16 '20 at 15:26
  • Given `productions = {NT_A, {{S_D, S_B}, {S_A, S_C}}};`, what do you expect? – Jarod42 Nov 16 '20 at 15:26
  • 1
    Does this answer your quesiton? https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects. Frankly, you again made the mistake of making it more complicated than necessary. There is a simple answer to "How to sort vectors of any type? (with a custom predicate)" and the answer is basically the same no matter what is the actual type of elements – 463035818_is_not_an_ai Nov 16 '20 at 15:27
  • the link I gave should help. What is unclear in your question is 1) what are the elements you want to sort? `Symbol`s? or `std::vector`s? 2) What determines if element `a` should come before element `b`? In other words you need to define `a < b`. Once you got that straight, you can apply answers from that link – 463035818_is_not_an_ai Nov 16 '20 at 15:32
  • idclev, you become my saviour :D but still, if i wanted to make the order an own explicit entity/attribute in c++, how would I proceed ? In Ruby it's an easy task. – von spotz Nov 16 '20 at 15:34
  • Oh, the innermost vector just by the order in which I added the items. – von spotz Nov 16 '20 at 15:35
  • Showing equivalent of what you want in ruby might help to understand what you want... – Jarod42 Nov 16 '20 at 15:35
  • I don't know Ruby, but maybe what you want is a comparator. Its an object, a callable, that specifies the order that is used for sorting – 463035818_is_not_an_ai Nov 16 '20 at 15:37

0 Answers0