I have two vectors. I want to iterate over all elements of both and do something (say print them out). So I could write something like:
vector<int> vec_a{1, 2, 3}, vec_b{4, 5, 6, 7};
for (auto a : vec_a) {
cout << a;
}
for (auto b : vec_b) {
cout << b;
}
This has a lot of duplication. I could do something like:
for (const auto& vec : {vec_a, vec_b}) {
for (auto elem : vec) {
cout << elem;
}
}
But this adds an extra for
(which is not too bad but I'm wondering if there is something better. Something along the lines of:
for (auto elem : concat(vec_a, vec_b)) {
cout << elem;
}
I know I could just concat the vectors (a la Concatenating two std::vectors) but that syntax is even clunkier (especially since I actually have 4 vectors).
I want the output to be:
1 2 3 4 5 6 7