I know that if I use a c++ container that implements the iterator
interface (provides begin()
and end()
functions) I can use a for loop like this to iterate over it:
for (auto element : container) {
process(element);
}
If I have two instances of the same type of container, I can write code like this
for (auto element : container1) {
process(element);
}
for (auto element : container2) {
process(element);
}
However this leads to repetitive code. I'm looking for a way to combine the two containers so that I can iterate over them at once (i.e. have the for loop iterate over the first one and then continue iterating over the second one). Something like this:
for (auto element : container1 + container2) {
process(element);
}
I know that I can use stuff like container1.insert(container1.end(), container2.begin(), container2.end());
to concatenate them if they happen to be vectors, but I want to be able to do this more generally, in place, with a single line, and without modifying either container.