C ++ 11 for container loop for multiple elements

I was wondering if it is possible, in C ++ 11 syntax, to use a new container to loop for multiple elements, like this:

std::vector<double> x;
std::vector<double> y;

for (double& xp, yp : x, y)
{
     std::cout << xp << yp << std::endl;
}

      

I was unable to find any information on using this loop for multiple containers. I would appreciate everyone's help.

An example of an effect in the classic for a 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
}

      

+3


source to share


2 answers


There is a request in the language working group to support a very similar syntax for repeating multiple containers at the same time:

Section : 6.5.4 [stmt.ranged] Status : Open Sender : Gabriel Dos Reis

Opened : 2013-01-12 Last modified : 2015-05-22

Discussion

The "for" syntax of the new style allows us to do without administrative iterator declarations when iterating through one sequence. However, the burden and noise persist when repeating several or more sequences at the same time. We need to expand the syntax to be able to do this. For example. you need to be able to write:

for (auto& x : v; auto& y : w)
  a = combine(v, w, a);

      

instead of 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 might happen, but not in the near future.

Meanwhile, the best choice is probably the classic one for the cycle.

+5


source


I know this is a bit old-fashioned now, but I had a similar request and TC "you need some help with the library," I said, grinning because I decided to solve a similar problem with only a few extra characters. To rework the OP's first example and assuming the vectors are guaranteed to be the same size, you can make the C ++ 11 reference counter-old-school pointer arithmetic, like this:

std::vector<double> x;
std::vector<double> y;

for (double &xp:x)
{
   std::cout << xp << y[&xp-&x[0]] << std::endl;
}

      



It's probably best not to use in production code (I'm a hobbyist teacher myself, so me), but it works well enough, simple, doesn't require any libraries, and doesn't seem to suffer any discernible speed (at least that I noticed ). It works even 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 needed (so any speed reduction is reduced in noise), but since y [] accessed was needed with the same index as in x [] and didn't require additional variables / iterators, it matched the vector perfectly.

Also, +1 for manlio's answer; would you believe that I was actually trying to use this particular syntax intuitively before I went looking for an alternative?: Oh

+2


source







All Articles