473,395 Members | 1,977 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,395 software developers and data experts.

A question about iterators.

I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
What have I done wrong here... Why couldn't I just call the method as:
x = method(im_it);

Aug 25 '05 #1
7 1175
I assume somewhere you have this :

typedef std::list<image> series ;
/// Or some other sequence container vector, deque ...

The type series::iterator is a type of object ( sometimes a pointer ),
which is used to iterate through a container. To get to the item that
the iterator points to you must first dereference it through operator*.
This gets you back to the object "image", since your function takes a
pointer to that object, you must then put "&" in front, to get the
pointer to it. Hence the need for "&*im_it".

In some implementations of std::vector a iterator is indeed a pointer,
but you should not rely on that, as switching to a different
implementation will cause your code to not compile. ( See Meyers
Effective STL for lots of discussion regarding this ).

I hope this helped.

-Art

Aug 25 '05 #2
JustSomeGuy wrote:
I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
Not the address of the contents. The address of the results of the
operator* applied to the iterator.
What have I done wrong here...
Nothing, judging from the minuscule amount of code you provided.
Why couldn't I just call the method as:
x = method(im_it);


You could if 'im_it' were convertible to 'image*'.

V
Aug 25 '05 #3
JustSomeGuy wrote:
I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
What have I done wrong here... Why couldn't I just call the method as:
x = method(im_it);


An interator may not be implemented as a pointer.

You should also post complete code.

Ben
--
I'm not just a number. To many, I'm known as a String...
Aug 25 '05 #4
"JustSomeGuy" <bo*******@yahoo.com> wrote in news:1124995116.298859.232630
@o13g2000cwo.googlegroups.com:
I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
What have I done wrong here... Why couldn't I just call the method as:
x = method(im_it);


Because series::iterator isn't necessarily a pointer. It may be a full
blown class. However, *im_it is required to be a reference to the
contained type. So, by doing a &*im_it you are getting a pointer to the
contained type.
Aug 25 '05 #5
On 2005-08-25 14:43:55 -0400, Andre Kostur <nn******@kostur.net> said:
"JustSomeGuy" <bo*******@yahoo.com> wrote in news:1124995116.298859.232630
@o13g2000cwo.googlegroups.com:
I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
What have I done wrong here... Why couldn't I just call the method as:
x = method(im_it);
Because series::iterator isn't necessarily a pointer. It may be a full
blown class. However, *im_it is required to be a reference to the
contained type.


<pedantic>
That, of course, depends on series::iterator's category. If it is
simply an Input Iterator, then there is no guarantee that *im_it is a
reference at all (operator*() may return by value, or it may return
some intermediate helper class, etc.).
</pedantic>
So, by doing a &*im_it you are getting a pointer to the contained type.


--
Clark S. Cox, III
cl*******@gmail.com

Aug 25 '05 #6
Clark S. Cox III <cl*******@gmail.com> wrote in
news:2005082517010916807%clarkcox3@gmailcom:
On 2005-08-25 14:43:55 -0400, Andre Kostur <nn******@kostur.net> said:
"JustSomeGuy" <bo*******@yahoo.com> wrote in
news:1124995116.298859.232630 @o13g2000cwo.googlegroups.com:
I have a method that takes a pointer to an object as a parameter.

method(image * i)
{
....
}
somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}

Notice the call to the method has the address of the contents of the
iterator?
What have I done wrong here... Why couldn't I just call the method
as: x = method(im_it);


Because series::iterator isn't necessarily a pointer. It may be a
full blown class. However, *im_it is required to be a reference to
the contained type.


<pedantic>
That, of course, depends on series::iterator's category. If it is
simply an Input Iterator, then there is no guarantee that *im_it is a
reference at all (operator*() may return by value, or it may return
some intermediate helper class, etc.).
</pedantic>


OK... granted. However somewhere else we were talking about a list<>
specifically... so I'm assuming a container, and not something like
istream_iterator :)
Aug 25 '05 #7
JustSomeGuy wrote:

somefunction(series *Ser)
{
series::iterator im_it;
for (im_it=Ser->begin(); im_it != Ser->end(); ++im_it)
{
x = method(&*im_it);
...
}
}


If you aren't going to check 'Ser' for NULL, then consider
passing by reference:

somefunction(series &ser)
{
for (series::iterator im_it = ser.begin();
im_it != ser.end(); ++im_it)
x = method(&*im_it);
}

Aug 25 '05 #8

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

Similar topics

24
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = ...
21
by: Christophe | last post by:
Is there a good reason why when you try to take an element from an already exausted iterator, it throws StopIteration instead of some other exception ? I've lost quite some times already because I...
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
90
by: John Salerno | last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part, but it does in the second? Is the first statement something different? False print 'hi' hi Thanks.
21
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
3
by: Jess | last post by:
Hello, Iterators are typically put into five different categories, namely input iterator, output iterator, forward iterator, bidirectional iterator and random iterator. The differences come...
11
by: Diego Martins | last post by:
for me, these items are in the 'tricky zone' of C++ does anyone know good material with that? (dealing with subtle details, pitfalls, good practices...) anything like the Effective series from...
5
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
2
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.