3 ms·
Just use two parallel containers: one for names, and one for values. It is dead simple to check that they have the same size and iterate over both simultaneous
by makecheck 11y ago
Just use two parallel containers: one for names, and one for values. It is dead simple to check that they have the same size and iterate over both simultaneously where needed. And the best part is, for code that does not actually require both pieces of information, you can pass one of the lists and pretend that the other does not exist. Code that only knows how to visit values only has to deal with values and not weird hybrid name/value elements.
Seriously, always try the simpler thing first. Do not go deeper into templates and special syntax before seeing if any other construct will do the job.
- daemin 11y agoOr you know, actually make a class that contains the data and a constructor. Or even a simple struct if one is allergic to classes for simple types. For the example given in the article a simple class or struct is already a better and easier data structure to use and store.
- nhatbui 11y agoIs this only appropriate for the case where we want to iterate over all the elements? Don't we lose the O(1) access using a name? I'm assuming you're making 2 arrays.
- makecheck 11y agoYes. My point is to consider what is actually required before making complex structures. It is often perfectly acceptable to combine two existing types and handle what few interactions they actually have.