473,394 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

why I can't use previous iterator?

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.
Jul 22 '05 #1
5 1672
"Hunter Hou" <hy***@lucent.com> wrote in message
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.
Jul 22 '05 #2
Hunter Hou wrote:
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
Jul 22 '05 #4


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.
sort( .... );
// print sorted vector elements
for( p = vecInt.begin(); p != vecInt.end(); p++ ) {
.......
}


Yes. I had same solution.
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.

Jul 22 '05 #5
"Hunter Hou" <hy***@lucent.com> wrote in message news:<ca********@netnews.proxy.lucent.com>...

[ ... ]
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::ostream_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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help...
26
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class...
4
by: Arturo Cuebas | last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa) begin/end pairs that point to various chunks in a file. It would be super-cool, in my program, to be able to treat all of these ranges...
3
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the...
3
by: vasili | last post by:
hello All, I have a simple issue. I defined a custom container, that encloses a std::list, which in turn holds objects that are a simple abstraction of a six position array. Now, i would like...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
1
by: Axel Gallus | last post by:
I built a container (without a template), an iterator as a nested class and a "find"-method: iterator has a friend relationship with Hash_Map and vice versa. class Hash_Map { iterator...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
0
by: Lie Ryan | last post by:
On Wed, 01 Oct 2008 10:46:33 -0400, Luis Zarrabeitia wrote: No (or I'm not aware of any). Why? Because for some iterable, it is not possible to know in advance its length (video data stream,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.