Can I use iterator in this way?



 DEVELOP > c-Plus-Plus > Can I use iterator in this way?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "mimi"
Date: 16 May 2007 03:29:40 AM
Object: 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.
.

User: "Ivan Vecerina"

Title: Re: Can I use iterator in this way? 16 May 2007 03:57:47 AM
"mimi" <cainiaodelixiang@gmail.com> wrote in message
news:1179304180.251436.257130@k79g2000hse.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
.
User: "Sylvester Hesp"

Title: Re: Can I use iterator in this way? 16 May 2007 04:27:11 AM
"Ivan Vecerina" <_INVALID_use_webform_@ivan.vecerina.com> wrote in message
news:41061$464ac78a$3e028af2$14883@news.hispeed.ch...

"mimi" <cainiaodelixiang@gmail.com> wrote in message
news:1179304180.251436.257130@k79g2000hse.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.

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


User: "peter koch"

Title: Re: Can I use iterator in this way? 16 May 2007 03:38:41 AM
On 16 Maj, 10:29, mimi <cainiaodelixi...@gmail.com> 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
.

User: "Stefan Naewe"

Title: Re: Can I use iterator in this way? 16 May 2007 03:37:00 AM
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
.
User: "mimi"

Title: Re: Can I use iterator in this way? 16 May 2007 03:59:05 AM
On 5=D4=C216=C8=D5, =CF=C2=CE=E74=CA=B137=B7=D6, Stefan Naewe <nos...@pleas=
e=2Enet> 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 =3D 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 =3D *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- =D2=FE=B2=D8=B1=BB=D2=FD=D3=

=C3=CE=C4=D7=D6 -


- =CF=D4=CA=BE=D2=FD=D3=C3=B5=C4=CE=C4=D7=D6 -

.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER