Can I use iterator in this way?

M

mimi

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.
 
S

Stefan Naewe

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.
 
P

peter koch

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
 
I

Ivan Vecerina

: 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
 
M

mimi

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

Sylvester Hesp

Ivan Vecerina said:
: 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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top