Thread: How is iterator implemented in vector class?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    48

    How is iterator implemented in vector class?

    Code:
    vector<int> v;
    vector<int>::iterator i = v.begin();
    How is iterator implemented?

    I mean,

    Code:
    test::box c = 1;
    Then how is box and test related? And how does test class looks like?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Roughly speaking, iterator would be a type definition within the (templated) definition of the vector class. It's slightly more complicated than that as, conceptually at least, vector can be a specialisation of some other class, or might have some base class. The type definition within the definition of vector might, itself, be a definition of a class or struct type, or it might be a simple typedef.

    Your "test::box" example is actually the more general case. test is generally a namespace, and box will be a type definition within that namespace. Classes and structs are associated with a namespace that has the same name (i.e. the member functions and data members of a class X exist in a namespace named X, which is why the X::member syntax works). Things are complicated somewhat, as test may also be an alias (eg resulting from typedef or macro expansion) of the actual class/namespace that contains the definition of box.

    Note: in your post, you ignored the fact that vector is a templated class within the std namespace. The relationship between the vector's iterator and the vector (template) class is the same as the relationship between the vector template and the std namespace. Namespaces can be nested: there can be namespaces within namespaces, struct/class definitions (and their associated namespaces) within namespaces, namespaces within struct/class definitions, and struct/class definitions within struct/class definitions.
    Last edited by grumpy; 06-09-2012 at 08:29 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    48
    I didn't get you.
    But Do you mean that iterator is class inside vector class? (And because we are accessing it using :: operator so it must be a static class)

    Something like this:

    Code:
    Template <class T>
    class vector
    {
         static class iterator
         {
    
          };
    
    };
    
    
    void main()
    {
        
    vector<int> v;
    vector<int>::iterator i = v.begin();
    
    
     }
    Last edited by freiza; 06-09-2012 at 10:00 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You need to read more closely. Many answers you get here will often concern underlying concepts, rather than just spoon-feeding you code to illustrate. Because describing the concepts is both more precise and less verbose.

    Anyway, to rephrase the first para of my previous post for the purpose of spoon-feeding .....

    vector::iterator is a type, not a member of vector. The :: means it is in a specified scope, not that it is a static class.

    One way might be
    Code:
    //within namespace std
    
    template < class T, class Allocator = allocator<T> >  class vector  // this is approximately what the standard specifies
    {
           public:
    
                class iterator {  <whatever> };      // this is a class definition
    };
    An alternative would be
    Code:
    //within namespace std
    
    template < class T, class Allocator = allocator<T> >  class vector
    {
           public:
    
                typedef some_type_related_to_T iterator;   
    };
    A particular case, which occurs in some (but not all) implementations of the standard library, is
    Code:
    //within namespace std
    
    template < class T, class Allocator = allocator<T> >  class vector
    {
           public:
    
                typedef T *iterator;   
                typedef const T * const_iterator;
                  // etc
    };
    This description is both longer and less precise than my previous post.
    Last edited by grumpy; 06-09-2012 at 10:22 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector and iterator problem
    By BeBu in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2009, 07:38 AM
  2. Vector Iterator Help
    By (TNT) in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2007, 02:53 PM
  3. stl vector + iterator operation
    By sujeet1 in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2007, 04:10 PM
  4. vector/iterator program
    By Drake in forum Game Programming
    Replies: 3
    Last Post: 02-06-2006, 01:55 PM