How can I get an address from STL's iterator?

This is a discussion on How can I get an address from STL's iterator? within the C++ Programming forums, part of the General Programming Boards category; Code: vector<int> vec; vec.push_back(1); vector<int>::iterator it = vec.begin(); Now I want to get item "1" 's address, Can I do: ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    How can I get an address from STL's iterator?

    Code:
    vector<int> vec;
    vec.push_back(1);
    vector<int>::iterator it = vec.begin();
    Now I want to get item "1" 's address, Can I do:

    Code:
    int* pint = &*it
    ?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Yes.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    The &*it looks awkward. Does vector iterator provide an off-the-shelf method?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    That's because you aren't supposed to take the address of something stored in an iterator.
    Iterators are C++'s version of pointers. They're much more flexible.
    So unless you have to - don't do it. There's a reason why there's no easy way.

    With a vector, you can do &vec[0], because it's guaranteed to be contiguous. Other containers are not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Does vector iterator provide an off-the-shelf method?
    No. It is rare that you would need a pointer to an item in a vector, so there's no reason for that to be part of the vector's direct interface.

    You have to get a reference to the object then take its address. There are other ways to get the reference to the object besides using an iterator. Here are a couple other examples:
    Code:
    vector<int> vec;
    vec.push_back(1);
    
    int* pint1 = &(*vec.begin());
    int* pint2 = &(vec.at(0));
    int* pint3 = &(vec[0]);
    
    vector<int>::iterator it1 = vec.begin();
    int* pint4 = &(*it1);
    
    int& rint1 = vec.at(0);
    int* pint5 = &(rint1);
    
    int& rint2 = vec[0];
    int* pint6 = &(rint1);
    Note that when you're not using a temporary or iterator (as in #5 and #6) you want to use a reference variable, otherwise you'll be making a copy and taking the address of that.

    >> With a vector, you can do &vec[0], because it's guaranteed to be contiguous. Other containers are not.
    This is true, but is not relevant to the question. You can do &vec[0] with any container that supports operator[] to get a pointer to the first element, which is what was being discussed.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Just a caution, because most of the time you may want a pointer to the first element is to pass it as a pointer to a function that expects that pointer to point to an array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, don't expect the pointer you create to stay valid forever, because STL containers can reallocate their internal memory when they need more space, and then you have a pointer that points to deallocated memory.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And finally, you can't do this stuff with vector<bool>, because it's completely broken.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  3. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 09:49 PM
  4. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 02:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21