Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I use iterator in this way?

Reply
Thread Tools

Can I use iterator in this way?

 
 
mimi
Guest
Posts: n/a
 
      05-16-2007
It seems that iterator could be treated as the pointer to object. But
I am quite doubt about it.
Is using &(*iterator) instead of the pointer to object(after copy the
object from the iterator) permitted, or appreciated?

#include <vector>

int main()
{
std::vector<int> vecFoo(4, 0);
std::vector<int>::iterater iter = vecFoo.begin();

//Some function need a pointer to int, should i use
someFunc( &(*iter)); //(1)

//Should i use the following to replace the (1)
int i = *iter;
someFunc(&i);

return 0;
}

Thanks for any advice.

 
Reply With Quote
 
 
 
 
Stefan Naewe
Guest
Posts: n/a
 
      05-16-2007
On 5/16/2007 10:29 AM, mimi wrote:
> It seems that iterator could be treated as the pointer to object. But
> I am quite doubt about it.
> Is using &(*iterator) instead of the pointer to object(after copy the
> object from the iterator) permitted, or appreciated?
>
> #include <vector>
>
> int main()
> {
> std::vector<int> vecFoo(4, 0);
> std::vector<int>::iterater iter = vecFoo.begin();
>
> //Some function need a pointer to int, should i use
> someFunc( &(*iter)); //(1)
>
> //Should i use the following to replace the (1)
> int i = *iter;
> someFunc(&i);
>
> return 0;
> }
>
> Thanks for any advice.
>


Use:

someFunc(&vecFoo[0]);

(see: Effective STL, Item 16)

S.
--
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      05-16-2007
On 16 Maj, 10:29, mimi <(E-Mail Removed)> wrote:
> It seems that iterator could be treated as the pointer to object. But
> I am quite doubt about it.
> Is using &(*iterator) instead of the pointer to object(after copy the
> object from the iterator) permitted, or appreciated?
>
> #include <vector>
>
> int main()
> {
> std::vector<int> vecFoo(4, 0);
> std::vector<int>::iterater iter = vecFoo.begin();
>
> //Some function need a pointer to int, should i use
> someFunc( &(*iter)); //(1)


This will work because your vector is not empty. If someFunc modifies
what the pointer points to, the contents of the vector will change to.
>
> //Should i use the following to replace the (1)
> int i = *iter;
> someFunc(&i);
>
> return 0;
>
> }

This will work to, but if someFunc changes what the pointer points to,
the vector will not change. So it comes down to what effect you're
trying to achieve.

/Peter

 
Reply With Quote
 
Ivan Vecerina
Guest
Posts: n/a
 
      05-16-2007
"mimi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: It seems that iterator could be treated as the pointer to object. But
: I am quite doubt about it.
: Is using &(*iterator) instead of the pointer to object(after copy the
: object from the iterator) permitted, or appreciated?
:
: #include <vector>
:
: int main()
: {
: std::vector<int> vecFoo(4, 0);
: std::vector<int>::iterater iter = vecFoo.begin();
:
: //Some function need a pointer to int, should i use
: someFunc( &(*iter)); //(1)

This will work for (almost*) all iterators for accessing (only)
the item pointed to by the iterator.
In the case of a vector<>::iterator *only*, you can even index
the resulting pointer to access adjacent items (within the
bounds of the vector). E.g. someFunc( (&*iter)+1 ) will
be the same as &vecFoo[1].

* Some exceptions and caveats:
- istream_iterator: the * operator will return a temporary
object. While it will be possible to access its address,
the temporary object will only be valid until the
completion of the statement.
- An item class could overload the address-of operator (&)
in and unexpected way, but this would probably cause other
failures anyway.

: //Should i use the following to replace the (1)
: int i = *iter;
: someFunc(&i);

Making a copy of the item is unnecessary in this case.


I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

 
Reply With Quote
 
mimi
Guest
Posts: n/a
 
      05-16-2007
On 5月16日, 下午4时37分, Stefan Naewe <(E-Mail Removed)> wrote:
> On 5/16/2007 10:29 AM, mimi wrote:
>
>
>
>
>
> > It seems that iterator could be treated as the pointer to object. But
> > I am quite doubt about it.
> > Is using &(*iterator) instead of the pointer to object(after copy the
> > object from the iterator) permitted, or appreciated?

>
> > #include <vector>

>
> > int main()
> > {
> > std::vector<int> vecFoo(4, 0);
> > std::vector<int>::iterater iter = vecFoo.begin();

>
> > //Some function need a pointer to int, should i use
> > someFunc( &(*iter)); //(1)

>
> > //Should i use the following to replace the (1)
> > int i = *iter;
> > someFunc(&i);

>
> > return 0;
> > }

>
> > Thanks for any advice.

>
> Use:
>
> someFunc(&vecFoo[0]);
>
> (see: Effective STL, Item 16)
>

Thank you very much. I have the impression of the Item but i don't
remember which item. Thanks a lot for reminding me that.
> S.
> --
> Stefan Naewe
> stefan dot naewe at atlas-elektronik dot com- 隐藏被引用文字 -
>
> - 显示引用的文字 -



 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      05-16-2007

"Ivan Vecerina" <(E-Mail Removed)> wrote in message
news:41061$464ac78a$3e028af2$(E-Mail Removed) ...
> "mimi" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> : It seems that iterator could be treated as the pointer to object. But
> : I am quite doubt about it.
> : Is using &(*iterator) instead of the pointer to object(after copy the
> : object from the iterator) permitted, or appreciated?
> :
> : #include <vector>
> :
> : int main()
> : {
> : std::vector<int> vecFoo(4, 0);
> : std::vector<int>::iterater iter = vecFoo.begin();
> :
> : //Some function need a pointer to int, should i use
> : someFunc( &(*iter)); //(1)
>
> This will work for (almost*) all iterators for accessing (only)
> the item pointed to by the iterator.
> In the case of a vector<>::iterator *only*, you can even index
> the resulting pointer to access adjacent items (within the
> bounds of the vector). E.g. someFunc( (&*iter)+1 ) will
> be the same as &vecFoo[1].
>
> * Some exceptions and caveats:
> - istream_iterator: the * operator will return a temporary
> object. While it will be possible to access its address,
> the temporary object will only be valid until the
> completion of the statement.
> - An item class could overload the address-of operator (&)
> in and unexpected way, but this would probably cause other
> failures anyway.


- ostream_iterator: the only valid use of *it is as in *it=value, so you
can't dereference it and expect to get a common reference.
- Dereferencing a std::vector<bool>::iterator does not yield a bool&, so
taking the address of the returned object is not a bool*.


- Sylvester


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I overload an iterator/create an iterator Pranav C++ 3 08-08-2008 06:49 AM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments