Reputation: 2949
I was wondering either it is possible in the c++11 syntax to use the new container based for loop for multiple items, for example:
std::vector<double> x;
std::vector<double> y;
for (double& xp, yp : x, y)
{
std::cout << xp << yp << std::endl;
}
I was not able to find any information about using this loop for more than one container. I would appreciate all help.
Example effect in the classic for loop:
std::vector<double>::iterator itX = m_x.begin();
std::vector<double>::iterator itY = m_y.begin();
for (uint32_t i = 0; i < m_x.size(); i++, itX++, itY++)
{
// operations on the m_x and m_y vectors
}
Upvotes: 3
Views: 2490
Reputation: 21
I know this is a little old now, but I had a similar query and T.C.'s "you'd need some library help" comment made me grin, because I ended up solving a similar issue with only a few extra characters. To recycle the OP's first example, and assuming as stated that the vectors are guaranteed the same size, you can make C++11 referencing meet old-school pointer arithmetic, like so:
std::vector<double> x;
std::vector<double> y;
for (double &xp:x)
{
std::cout << xp << y[&xp-&x[0]] << std::endl;
}
Probably best not used in production code (I'm a self-taught hobbyist coder, so meh) but it works well enough, is simple, requires no libraries and does not seem to suffer any notable speed penalty (at least, that I've noticed). It even works in minimal C++11 environments (such as VC11). To be clear, I used this in a slightly different context, where (given the above example) I only accessed y[] when a change was actually required (so any possible speed reduction is mitigated in the noise), but since the y[] access was required at the same index as in x[] and required no extra variables/iterators, it fit the bill perfectly.
Also, +1 for manlio's answer; would you believe I actually tried to use exactly that syntax intuitively before I went looking for an alternative? :O
Upvotes: 2
Reputation: 18902
There is a request in the language working group to support a very similar syntax to iterate simultaneously on many containers:
Section: 6.5.4 [stmt.ranged] Status: Open Submitter: Gabriel Dos Reis
Opened: 2013-01-12 Last modified: 2015-05-22
Discussion:
The new-style 'for' syntax allows us to dispense with administrative iterator declarations when iterating over a single sequence. The burden and noise remain, however, when iterating over two or more sequences simultaneously. We should extend the syntax to allow that. E.g. one should be able to write:
for (auto& x : v; auto& y : w) a = combine(v, w, a);
instead of the noisier
auto p1 = v.begin(); auto q1 = v.end(); auto p2 = w.begin(); auto q2 = w.end(); while (p1 < q1 and p2 < q2) { a = combine(*p1, *p2, a); ++p1; ++p2; }
See http://cplusplus.github.io/EWG/ewg-active.html#43
So it could happen but not in the near future.
Meanwhile the best choice is probably the classical for loop.
Upvotes: 5