I have the following pattern repeating in several different places in my code:
if (m_condition)
{
auto& item = m_vector[m_index];
Do something with 'item'
}
else
{
for (auto& item : m_vector)
{
Do something with 'item'
}
}
Where m_condition, m_vector and m_index are class members.
The Do something with 'item' part is different in each occurrence of the pattern.
I am looking for a nicer/cleaner/shorter way to implement this (performance is not an issue).
I came up with this, but it feels kinda "nasty":
auto iBgnIndex = m_condition ? m_index : 0;
auto iEndIndex = m_condition ? m_index : m_vector.size()-1;
for (auto i = iBgnIndex; i <= iEndIndex; i++)
{
Do something with 'm_vector[i]'
}
What better options do I have?
Thanks
Aucun commentaire:
Enregistrer un commentaire