 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
mimi Guest
|
Posted: Wed May 16, 2007 9:10 am Post subject: Can I use iterator in this way? |
|
|
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. |
|
| Back to top |
|
 |
Stefan Naewe Guest
|
Posted: Wed May 16, 2007 9:10 am Post subject: Re: Can I use iterator in this way? |
|
|
On 5/16/2007 10:29 AM, mimi wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
peter koch Guest
|
Posted: Wed May 16, 2007 9:10 am Post subject: Re: Can I use iterator in this way? |
|
|
On 16 Maj, 10:29, mimi <cainiaodelixi...@gmail.com> wrote:
| Quote: | 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.
| Quote: |
//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 |
|
| Back to top |
|
 |
Ivan Vecerina Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: Can I use iterator in this way? |
|
|
"mimi" <cainiaodelixiang (AT) gmail (DOT) com> wrote in message
news:1179304180.251436.257130 (AT) k79g2000hse (DOT) googlegroups.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 |
|
| Back to top |
|
 |
mimi Guest
|
Posted: Wed May 16, 2007 9:11 am Post subject: Re: Can I use iterator in this way? |
|
|
On 5月16日, 下午4时37分, Stefan Naewe <nos...@please.net> wrote:
| Quote: | 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.
| Quote: | S.
--
Stefan Naewe
stefan dot naewe at atlas-elektronik dot com- 隐藏被引用文字 -
- 显示引用的文字 - |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|