I have some code that looks something like this:
bool var = some condition...
if( var )
{
for( int i=0; i<10; ++i )
{
}
}
else
{
for( int i=9; i>=0; --i )
{
}
}
However, the code that must be executed inside the for loop is almost identical, so I don’t want to write it twice. I know I can do something like this:
bool var = some condition...
for( int i = (var ? 0 : 9 ); (var ? i<10 : i>=0 ); (var ? ++i : --i ) )
{
}
But this is really not an elegant solution.
Is there a short, more elegant way to do this? I checked std :: iterator, but I don't think what I need.
source
share