How do vector choose const or non-const?

Discussion in 'C++' started by Nephi Immortal, Sep 2, 2011.

  1. I have a question. If I declare either const or non-const iterator
    object, how do vector constructor know to recognize const or non-
    const?

    For example:

    vector< int > v;
    vector< int >::iterator I_B = v.begin();
    vector< int >::iterator I_E = v.end();

    const vector< int > cv;
    vector< int >::const_iterator cI_B = cv.begin();
    vector< int >::const_iterator cI_E = cv.end();

    Look at the function declaration below.

    template<class InputIterator>
    vector(
    InputIterator _First,
    InputIterator _Last
    );

    I write another vector constructor.

    vector< int > v2( I_B, I_E );
    const vector< int > cv2( cI_B, cI_E );

    How do vector constructor recognize? It sounds like it must have
    first version of const constructor function and second version of non-
    const constructor function.
     
    Nephi Immortal, Sep 2, 2011
    #1
    1. Advertising

  2. On Sep 2, 6:46 pm, Sam <> wrote:
    > Nephi Immortal writes:
    > > I write another vector constructor.

    >
    > > vector< int > v2( I_B, I_E );
    > > const vector< int > cv2( cI_B, cI_E );

    >
    > > How do vector constructor recognize?  It sounds like it must have
    > > first version of const constructor function and second version of non-
    > > const constructor function.

    >
    > There's just one constructor. There's no such thing as a const contructoror  
    > a non-const constructor. In both cases, you are constructing a  
    > std::vector<int>. The only difference here is that after your vector is  
    > constructed the resulting object is either mutable or constant.
    >
    > This particular constructor is actually a template. It'll take any pair of  
    > iterators, as long as the values they iterate over are convertible to a  
    > std::int.
    >
    >  application_pgp-signature_part
    > < 1KViewDownload


    I understand, but I am trying to write vector and iterator from
    scratch in order to have better understanding how they behave. Can
    you please demonstrate your simple basic code here and tell me how one
    constructor is called with either mutable or constant.

    I suspect that iterator class must have two versions. First one is
    const_iterator class and second one is iterator class. vector is only
    one version class to call either const_iterator class or iterator
    class.

    If iterator has two versions, it means that I don’t need to write
    “const iterator< … >” and “iterator< … >” is used. It sounds like
    that each member functions don’t need const after parameters.
     
    Nephi Immortal, Sep 3, 2011
    #2
    1. Advertising

  3. Nephi Immortal

    James Kanze Guest

    On Sep 3, 12:46 am, Sam <> wrote:
    > Nephi Immortal writes:
    > > I write another vector constructor.


    > > vector< int > v2( I_B, I_E );
    > > const vector< int > cv2( cI_B, cI_E );


    > > How do vector constructor recognize? It sounds like it must have
    > > first version of const constructor function and second version of non-
    > > const constructor function.


    > There's just one constructor.


    You mean that there's just one constructor template. There are
    an infinite number of constructors, since there are an infinite
    number of possible instantiations of the template. (Not to
    mention the other constructors.)

    > There's no such thing as a const contructor or
    > a non-const constructor. In both cases, you are constructing a
    > std::vector<int>. The only difference here is that after your vector is
    > constructed the resulting object is either mutable or constant.


    > This particular constructor is actually a template. It'll take any pair of
    > iterators, as long as the values they iterate over are convertible to a
    > std::int.


    And that both iterators have the same type. What doesn't work
    is something like:

    std::const_iterator start = v.begin();
    std::vector<int> v2( start, v.end() );

    --
    James Kanze
     
    James Kanze, Sep 3, 2011
    #3
  4. Nephi Immortal

    James Kanze Guest

    On Sep 3, 3:47 am, Sam <> wrote:
    > Nephi Immortal writes:


    [...]
    > Well, yes, const_iterator and iterator are two separate classes. However,
    > there's one template constructor that takes a beginning and an ending
    > iterator. Roughly speaking, it would look like this:


    > template<typename iter_type>
    > vector(iter_type beg_iter, iter_type end_iter)
    > {
    > while (beg_iter != end_iter)
    > push_back(*beg_iter++);
    > }


    > Of course, std::vector also has an allocator object, I'm leaving out a lot
    > of detail here. With the above constructor, you can give it either an
    > iterator or a const_iterator. Doesn't matter. Works with either one.


    The real problem is making it give the desired results for
    something like "std::vector<int> v(10, 1);". This isn't an
    exact match for the std::vector<int>(size_t, int), and is an
    exact match for the template constructor above. And of course,
    there are also the complexity constraints when the iterator is a
    random_access_iterator.

    Implementing this constructor correctly is a non-trivial task,
    and really requires a very experiences C++ programmer. (On the
    other hand, something like what you've shown is a very good
    learning experiment, provided one does understand the
    restrictions.)

    --
    James Kanze
     
    James Kanze, Sep 3, 2011
    #4
    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. pmatos
    Replies:
    6
    Views:
    23,947
  2. Replies:
    8
    Views:
    1,963
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    585
    James Kanze
    Sep 4, 2007
  4. fungus
    Replies:
    13
    Views:
    901
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    545
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page