-2

I have a piece of code that was written by someone else before the New ISO come into effect.

The for LOOP in for (pa=a.begin(), i=0; pa != a.end(); ++pa) has a little trouble executing because of the i=0 part of the syntax. Also, I had to prefix the other for loop syntaxes to read for ( int i .....) with the int before the i. However, I don't know how to fix the int i=0 in this line: for (pa=a.begin ( ), i=0; pa != a.end ( ); ++pa). Please help me out.

  for ( int i = 0; pa != a.end(); ++pa)
      *pa = ++i;

  for (int i=0; i<10; i++)
      std::cout << "a[" << i << "]=" << a[i] << std::endl;

  // int i;  // note that this will work, but I do not want this extra line.

  for (pa=a.begin(), i=0; pa != a.end(); ++pa)
      std::cout << "a[" << i++ << "]=" << *pa << std::endl;
GSerg
  • 71,102
  • 17
  • 141
  • 299
Zzz Zz
  • 103
  • 1
  • 5
  • It was written before the first standard was finished in 1997. And I doubt that just because you don't want that extra line, people will change the language to not require it... – PlasmaHH Jul 04 '12 at 12:44
  • You could use a `std::pair` or a struct, but that line is a lot easier to read. – chris Jul 04 '12 at 12:45
  • Put that extra line, cringe a little and move on. – jrok Jul 04 '12 at 12:47
  • points well taken, but I was wondering if there is a more "compact" syntax, something to the effects of : for (pa=a.begin(), int i=0; .......), but the int i=0; declaration here won't work because of the comma preceding the "int". Any further clarification and help will be greatly appreciated. – Zzz Zz Jul 04 '12 at 12:49
  • See here: http://stackoverflow.com/questions/2687392/is-it-possible-to-declare-two-variables-of-different-types-in-a-for-loop?lq=1 – jrok Jul 04 '12 at 12:50
  • Check this stack overflow link : http://stackoverflow.com/questions/2687392/is-it-possible-to-declare-two-variables-of-different-types-in-a-for-loop – Santhosh Jul 04 '12 at 13:12
  • That *New ISO* has something like 14 years. The *new new ISO* was approved last year. – David Rodríguez - dribeas Jul 04 '12 at 13:17
  • possible duplicate of [Can I declare variables of different types in the initialization of a for loop?](http://stackoverflow.com/questions/8644707/can-i-declare-variables-of-different-types-in-the-initialization-of-a-for-loop) – Mechanical snail Aug 17 '12 at 05:23

2 Answers2

4

An extra declaration outside the for loop is the only sensible way to have two iteration variables of unrelated types in C++98 and later versions of the language. The initialiser can either be a single expression or a single declaration, and a declaration can't declare variables of multiple unrelated types.

If you really want a one-liner in this situation, then you could use this monstrosity:

for (int i = ((pa = a.begin()), 0); pa != a.end(); ++pa, ++i)

If you do that sort of thing regularly, then make sure that no-one who maintains your code knows where you live.

Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
3

No, you can declare only variables of one type inside the for. If your problem is scope then you may enclose the loop inside a block (for some nice examples look boost source code for loop macros):

for ( int i = 0; pa != a.end(); ++pa)
    *pa = ++i;

for (int i=0; i<10; i++)
    std::cout << "a[" << i << "]=" << a[i] << std::endl;

{
    int i = 0;
    for (pa=a.begin(); pa != a.end(); ++pa)
        std::cout << "a[" << i++ << "]=" << *pa << std::endl;
}

If you simply want to make it more nice then the answer is no, you can't.

EDIT
The best trick I saw to do what you need and to keep it clear is on this answer on SO. Instead of multiple variables you can use an unnamed struct declared inline:

for (struct { int i; iterator it; } d = { 0, pa.begin() }; d.it != a.end(); ++d.i, ++d.it )
    std::cout << "a[" << d.i << "]=" << *d.it << std::endl;

It's a little bit more prolix (so I wonder if what you save with the extra line is re-payed) but you make clear your intent and you keep code readable (moreover you can use it to pack any number and any type of variables).

Community
  • 1
  • 1
Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190