我在代码中的几个不同位置重复了以下模式:
if (m_condition)
{
auto& item = m_vector[m_index];
// Do something with 'item'
}
else
{
for (auto& item : m_vector)
{
// Do something with 'item'
}
}
m_condition
,m_vector
和m_index
是班级成员。
Do something with 'item'
部分在每次出现模式时都不同。
我正在寻找一种更好/更清洁/更短的方式来实现这一点(性能不是问题)。
我想出了这个,但感觉有点“讨厌”:
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]'
}
我有哪些更好的选择?
由于
答案 0 :(得分:1)
这很棘手,因为这种类型的重复模式最好在稍微更具体的上下文中解决。做一件事&#34;一件事或一件事&#34;一般来说很难写出来。
话虽如此,这里有一个解决方案,可以在你的情况下使用索引(为方便起见)和自定义或标准迭代器(为了清楚起见,我将索引编码为int
):
template <typename IndexedContainer_, typename UnaryOp_>
void apply_for_index(const IndexedContainer_& c, int index, UnaryOp_ op)
{
if (index == -1)
for (auto& elem : c) op(elem);
else
op(c[index]);
}
template <typename ForwardContainer_, typename Iterator_, typename UnaryOp_>
void apply_for_iter(const ForwardContainer_& c, Iterator_ it, UnaryOp_ op)
{
if (it == c.end())
for (auto& elem : c) op(elem);
else
op(*it);
}
以下是一些示例用法:
int main(int argc, char *argv[])
{
std::vector<int> elems = {0, 1, 2, 3, 4, 5};
printf("All:\n");
bool cond = false;
int four_or_all = cond ? 4 : -1;
apply_for_index(elems, four_or_all, [](int elem) {
printf("elem=%d\n", elem);
});
printf("\nSpecifically 4:\n");
four_or_all = !cond ? 4 : -1;
apply_for_index(elems, four_or_all, [](int elem) {
printf("elem=%d\n", elem);
});
printf("\nAll with iterators:\n");
auto four_or_all_iter = cond ? elems.begin() + 4 : elems.end();
apply_for_iter(elems, four_or_all_iter, [](int elem) {
printf("elem=%d\n", elem);
});
printf("\nSpecifically 4 with iterators:\n");
four_or_all_iter = !cond ? elems.begin() + 4 : elems.end();
apply_for_iter(elems, four_or_all_iter, [](int elem) {
printf("elem=%d\n", elem);
});
return 0;
}
正如您所看到的,当您在此级别解决问题时,您最终会使用某种难以阅读的标记或标记值。