why I can't use previous iterator?

H

Hunter Hou

Hi,

I just ran into one simple but confusing problem. Here is my code skeleton:

int main()
{
vector<int> vecInt( 5 );
typedef vector<int>::interator VI
for( VI p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}

sort( .... );
// print sorted vector elements
for( p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}
.......
return 0;
}


The compiler complains that:

sort.cpp: In function `int main()':
sort.cpp:62: name lookup of `p' changed for new ISO `for' scoping
sort.cpp:53: using obsolete binding at `p'

It's very confusing here sine I re-initialize p by begin(). who knows the
reson of this? what is the most elegant way of using 'p' here?(don't tell me
using [] operator instead :) )

Thanks,
Hunter
===
P.S:I solved this error by re-define p as exactly same as the first "for"
loop, which I don't like.
 
S

Siemel Naran

Hunter Hou said:
for( VI p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}

In the ANSI rules, the variable 'p' lives only in the for loop. After the
for loop is finished the variable 'p' does not exist, and you can't refer to
it.
sort( .... );
// print sorted vector elements
for( p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}

Solution 1 is to define the type of p again, as after all it's a new
variable

for(VI p = vecInt.begin(); p != vecInt.end(); p++ ) {

You can also declare

VI p;

at the top of the function. The first way is more C++ as you declare and
initialize the variable at the same time.
 
I

Ioannis Vranos

Hunter said:
Hi,

I just ran into one simple but confusing problem. Here is my code skeleton:

int main()
{
vector<int> vecInt( 5 );
typedef vector<int>::interator VI


typedef vector<int>::iterator VI;


for( VI p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}

sort( .... );
// print sorted vector elements
for( p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}
.......
return 0;
}



It can be more efficient to use ++p above because iterator is a
system-defined type (can be just a pointer but it can be otherwise) and
if it is a class with operator++() and operator++(int) defined, there is
performance difference (in case of a class, postfix operator keeps
another copy of the previous value inside it, increases the current
value and returns the previous, while prefix operator increases the
current value and returns it).



The compiler complains that:

sort.cpp: In function `int main()':
sort.cpp:62: name lookup of `p' changed for new ISO `for' scoping
sort.cpp:53: using obsolete binding at `p'

It's very confusing here sine I re-initialize p by begin(). who knows the
reson of this? what is the most elegant way of using 'p' here?(don't tell me
using [] operator instead :) )


You have defined p inside the scope of a for loop so it ceases to exist
after that scope.

You can define another p in the second loop:

for(VI p = vecInt.begin(); p != vecInt.end(); ++p)
{
//.......
}

P.S:I solved this error by re-define p as exactly same as the first
"for" loop, which I don't like.


Well you should like, because this is the best use.






Best regards,

Ioannis Vranos
 
H

Hunter Hou

In the ANSI rules, the variable 'p' lives only in the for loop. After the
for loop is finished the variable 'p' does not exist, and you can't refer to
it.

Thanks! I got it. Will never fall at the same place.

Yes. I had same solution.
 
J

Jerry Coffin

[ ... ]
for( VI p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}

As of this close-brace, p goes out of scope, and its lifetime ends.

.... but here you attempt to use p again, even though it no longer
exists:
for( p = vecInt.begin(); p != vecInt.end(); p++ ) {

Re-defining p is one way to fix this. A better one is to get rid of
this loop completely:

std::copy(vecInt.begin(), vecInt.end(),
std::eek:stream_iterator<int>(std::cout));

In fact, I'd go so far as to say that _anytime_ you use a loop to
iterate over the contents of a collection, it's probably at least
somewhat suspect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top