Can I use iterator in this way?

Discussion in 'C++' started by mimi, May 16, 2007.

  1. mimi

    mimi Guest

    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.
    mimi, May 16, 2007
    #1
    1. Advertising

  2. mimi

    Stefan Naewe Guest

    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
    Stefan Naewe, May 16, 2007
    #2
    1. Advertising

  3. mimi

    peter koch Guest

    On 16 Maj, 10:29, 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)


    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
    peter koch, May 16, 2007
    #3
  4. "mimi" <> wrote in message
    news:...
    : 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
    Ivan Vecerina, May 16, 2007
    #4
  5. mimi

    mimi Guest

    On 5ÔÂ16ÈÕ, ÏÂÎç4ʱ37·Ö, Stefan Naewe <> 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- Òþ²Ø±»ÒýÓÃÎÄ×Ö -
    >
    > - ÏÔʾÒýÓõÄÎÄ×Ö -
    mimi, May 16, 2007
    #5
  6. "Ivan Vecerina" <> wrote in message
    news:41061$464ac78a$3e028af2$...
    > "mimi" <> wrote in message
    > news:...
    > : 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
    Sylvester Hesp, May 16, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,398
  2. greg
    Replies:
    6
    Views:
    443
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    625
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,128
    Steven D'Aprano
    Apr 20, 2007
  5. Pranav
    Replies:
    3
    Views:
    681
    ManicQin
    Aug 8, 2008
Loading...

Share This Page