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;
An extra declaration outside the
forloop 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:
If you do that sort of thing regularly, then make sure that no-one who maintains your code knows where you live.