Confused by const_iterator

Discussion in 'C++' started by magnus.moraberg@gmail.com, Feb 24, 2009.

  1. Guest

    Hi,

    I have the following code which fails to compile -

    MField<int> mFieldInt;
    mFieldInt.insert(mFieldInt.begin(),10);

    because mFieldInt.begin() returns a const_iterator. The following code
    does compile -

    MField<int>::iterator i; // how do I initialize i?
    mFieldInt.insert(i,10);

    MField<int> stores a number of ints internally using a vector. Here's
    the specification for MField -

    http://www.h3dapi.org/uploads/api/H3DAPI_20_beta/docs/H3DAPI/html/classH3D_1_1MField.html

    My question though is, why does MField have an insert function
    associated with it if its begin function returns a const_iterator? How
    can one insert something into a MField object when the only function
    that returns an iterator (instead of a const_iterator) is the insert
    function itself. I'm totally confused by this.

    How can I insert a value at the start of an MField object's internal
    vector?

    Thanks for your help,

    Barry
     
    , Feb 24, 2009
    #1
    1. Advertising

  2. Guest

    On 24 Feb., 10:32, wrote:
    > Hi,
    >
    > I have the following code which fails to compile -
    >
    > MField<int> mFieldInt;
    > mFieldInt.insert(mFieldInt.begin(),10);
    >
    > because mFieldInt.begin() returns a const_iterator. The following code
    > does compile -
    >
    > MField<int>::iterator i;  // how do I initialize i?
    > mFieldInt.insert(i,10);
    >
    > MField<int> stores a number of ints internally using a vector. Here's
    > the specification for MField -
    >
    > http://www.h3dapi.org/uploads/api/H3DAPI_20_beta/docs/H3DAPI/html/cla...
    >
    > My question though is, why does MField have an insert function
    > associated with it if its begin function returns a const_iterator? How
    > can one insert something into a MField object when the only function
    > that returns an iterator (instead of a const_iterator) is the insert
    > function itself. I'm totally confused by this.
    >
    > How can I insert a value at the start of an MField object's internal
    > vector?


    It seems as if the API of MField is somehow broken. As I'm not
    familiar with the H3D API it cannot say how to fix it, but my guess is
    that MField needs additional member functions MField::begin () and
    MField::end () in order to be properly used (either those methods
    should be added to the interface, or the insert method which takes
    iterators should be deleted, so that MField provides read-only access
    to its values).

    However, such a question is not of interest in this newsgroup but
    should be asked by the programmers of the H3D API (perhaps you should
    post to forum of the H3D home page). All I can say is that the API
    seems somehow inconsistent to me. But keep in mind that this is an
    open project that lives on contributions, so (A) you shouldn't be
    surprised to find some bugs, and (B) are free to contribute your own
    bug fixes.

    Regards,
    Stuart
     
    , Feb 24, 2009
    #2
    1. Advertising

  3. Guest

    On 24 Feb, 11:00, wrote:
    > On 24 Feb., 10:32, wrote:
    >
    >
    >
    > > Hi,

    >
    > > I have the following code which fails to compile -

    >
    > > MField<int> mFieldInt;
    > > mFieldInt.insert(mFieldInt.begin(),10);

    >
    > > because mFieldInt.begin() returns a const_iterator. The following code
    > > does compile -

    >
    > > MField<int>::iterator i;  // how do I initialize i?
    > > mFieldInt.insert(i,10);

    >
    > > MField<int> stores a number of ints internally using a vector. Here's
    > > the specification for MField -

    >
    > >http://www.h3dapi.org/uploads/api/H3DAPI_20_beta/docs/H3DAPI/html/cla...

    >
    > > My question though is, why does MField have an insert function
    > > associated with it if its begin function returns a const_iterator? How
    > > can one insert something into a MField object when the only function
    > > that returns an iterator (instead of a const_iterator) is the insert
    > > function itself. I'm totally confused by this.

    >
    > > How can I insert a value at the start of an MField object's internal
    > > vector?

    >
    > It seems as if the API of MField is somehow broken. As I'm not
    > familiar with the H3D API it cannot say how to fix it, but my guess is
    > that MField needs additional member functions MField::begin () and
    > MField::end () in order to be properly used (either those methods
    > should be added to the interface, or the insert method which takes
    > iterators should be deleted, so that MField provides read-only access
    > to its values).
    >
    > However, such a question is not of interest in this newsgroup but
    > should be asked by the programmers of the H3D API (perhaps you should
    > post to forum of the H3D home page). All I can say is that the API
    > seems somehow inconsistent to me. But keep in mind that this is an
    > open project that lives on contributions, so (A) you shouldn't be
    > surprised to find some bugs, and (B) are free to contribute your own
    > bug fixes.
    >
    > Regards,
    > Stuart


    Thanks for your reply. I'm quite new to C++ and the code didn't make
    sense to me. Thanks for confirming my suspicions.
     
    , Feb 24, 2009
    #3
    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. Tim Partridge
    Replies:
    5
    Views:
    438
    Buster Copley
    Jul 24, 2003
  2. john smith

    const_iterator

    john smith, Aug 5, 2003, in forum: C++
    Replies:
    5
    Views:
    6,660
    Uwe Schnitker
    Aug 5, 2003
  3. CoolPint
    Replies:
    3
    Views:
    737
    CoolPint
    Dec 13, 2003
  4. Gernot Frisch

    incrementiing const_iterator

    Gernot Frisch, Sep 15, 2004, in forum: C++
    Replies:
    4
    Views:
    574
    Mike Wahler
    Sep 15, 2004
  5. Joseph Turian
    Replies:
    5
    Views:
    318
    Victor Bazarov
    Aug 2, 2005
Loading...

Share This Page