Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Confused by const_iterator

Reply
Thread Tools

Confused by const_iterator

 
 
magnus.moraberg@gmail.com
Guest
Posts: n/a
 
      02-24-2009
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/H3...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
 
Reply With Quote
 
 
 
 
DerTopper@web.de
Guest
Posts: n/a
 
      02-24-2009
On 24 Feb., 10:32, (E-Mail Removed) 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/H3...3DAPI/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
 
Reply With Quote
 
 
 
 
magnus.moraberg@gmail.com
Guest
Posts: n/a
 
      02-24-2009
On 24 Feb, 11:00, (E-Mail Removed) wrote:
> On 24 Feb., 10:32, (E-Mail Removed) 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/H3...3DAPI/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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot make const_iterator of templatized hash_map Joseph Turian C++ 5 08-02-2005 08:17 PM
incrementiing const_iterator Gernot Frisch C++ 4 09-15-2004 02:43 PM
Questions about Getting const_iterator out of iterator class CoolPint C++ 3 12-13-2003 04:13 AM
const_iterator john smith C++ 5 08-05-2003 10:35 AM
why is map::begin() returning const_iterator? Tim Partridge C++ 5 07-24-2003 08:21 PM



Advertisments