第一種很直覺,不過跟C++11提倡的Range based loop算是一種退化 而第二種出現了4個array,一不小心還會有打錯字的風險 天才作者提出了第三種方式
1 2 3 4 5
for (auto& item : iter::eraser(array)) { if (*item == value) // Access item via deref item.mark_for_erase(); // Item is marked for deletion, but is still valid until end of loop iteration }
template<typename U, typename ... T> boolone_of(U&& u, T && ... t) { bool match = false; (void)std::initializer_list<bool>{ (match = match || u == t)... }; return match; } if (one_of(thing.x, 1, 2, 3)) dosomething();
template<typename U, typename ... T> boolone_of(U&& u, T && ... t) { bool match = false; autolist = std::initializer_list<bool>{ (match = match || u == t)... }; for (auto v : list) std::cout << v << std::endl; return match; }