c++ - What is a concise syntax for accessing the other element of two element collection? -
i have 2-element collection {a, b}
, complex expression ex(_, _)
involving both elements in non-commutative way. result of computation i'm looking equivalent to:
ex(a, b); ex(b, a);
but instead of repeating whole expression twice (defining function computing ex
not acceptable), use loop this:
for (auto i: {a, b}) ex(i, <other element>);
what short way of writing such loop?
edit
since number of readers found question unclear, please have @ 2 answers below, present solutions looking for. ex(a, b)
shorthand long chunk of code using a
, b
in order. reasons why don't want define function ex
stylistic , beyond scope of question.
if elements indexed 0 , 1 (such in vector v) use exclusive or ^
select alternative index:
for(auto i: {0, 1}) ex(v[i], v[i ^ 1]);
Comments
Post a Comment