A hard code solution would be:
for (int a1 : v) {
for (int a2 : v) {
for (int a3 : v) {
for (int a4 : v) {
for (int a5 : v) {
do_job(a1, a2, a3, a4, a5);
}
}
}
}
}
You may use the following for a generic way (putting all a
s into vector):
bool increase(std::size_t n, std::vector<std::size_t>& its)
{
for (auto rit = its.rbegin(); rit != its.rend(); ++rit) {
++*rit;
if (*rit >= n) {
*rit = 0;
} else {
return true;
}
}
return false;
}
template <typename F, typename C>
void iterate(F f, const C& v, std::size_t k)
{
std::vector<std::size_t> it(k, 0);
do {
f(v, it);
} while (increase(v.size(), it));
}
Live Demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…