Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Accessing member via set's NON-const iterator that doesn't affect invariants

Reply
Thread Tools

Accessing member via set's NON-const iterator that doesn't affect invariants

 
 
Anand Hariharan
Guest
Posts: n/a
 
      08-30-2012
Let's say I have code like so:

#include <set>
#include <math.h>

typedef int OtherTypes;

struct MyType
{
double Field1;
OtherTypes MoreFields;

MyType(double blah) :
Field1(blah)
{
}

bool operator < (const MyType &That) const
{
// Does not use any other member
return ( fabs(Field1 - That.Field1) > 1e-6 &&
Field1 < That.Field1 );
}
};

int main()
{
std::set<MyType> foo;
std:air< std::set<MyType>::iterator,
bool > inchk = foo.insert(MyType(1.0));

OtherTypes SomeVal = 1;
if ( inchk.second )
inchk.first->MoreFields = SomeVal; // error
}


How do I reassure the compiler that writing MoreFields will not affect
any invariants or will not do anything to invalidate the order of
elements in the set?

If the only recourse is to use another container such as vector, how
do I insert a new value in the sorted position while checking if one
exists already?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-30-2012
On 8/30/2012 1:14 PM, Anand Hariharan wrote:
> Let's say I have code like so:
>
> #include <set>
> #include <math.h>
>
> typedef int OtherTypes;
>
> struct MyType
> {
> double Field1;
> OtherTypes MoreFields;
>
> MyType(double blah) :
> Field1(blah)
> {
> }
>
> bool operator < (const MyType &That) const
> {
> // Does not use any other member
> return ( fabs(Field1 - That.Field1) > 1e-6 &&
> Field1 < That.Field1 );
> }
> };
>
> int main()
> {
> std::set<MyType> foo;
> std:air< std::set<MyType>::iterator,
> bool > inchk = foo.insert(MyType(1.0));
>
> OtherTypes SomeVal = 1;
> if ( inchk.second )
> inchk.first->MoreFields = SomeVal; // error
> }
>
>
> How do I reassure the compiler that writing MoreFields will not affect
> any invariants or will not do anything to invalidate the order of
> elements in the set?


Perhaps you should declare 'MoreFields' "mutable"?

> If the only recourse is to use another container such as vector, how
> do I insert a new value in the sorted position while checking if one
> exists already?


If your vector is sorted, use 'lower_bound' (or is it 'upper_bound'?) to
find the place where the sorting would place your element, then compare
what you want to insert to what's already there (the next element or the
previous, can't divine now) thus /checking/ if one exists.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Anand Hariharan
Guest
Posts: n/a
 
      08-30-2012
On Aug 30, 12:25*pm, Victor Bazarov <(E-Mail Removed)> wrote:
> On 8/30/2012 1:14 PM, Anand Hariharan wrote:
>
>
>
>
>
>
>
>
>
> > Let's say I have code like so:

>
> > #include <set>
> > #include <math.h>

>
> > typedef int OtherTypes;

>
> > struct MyType
> > {
> > * * *double Field1;
> > * * *OtherTypes MoreFields;

>
> > * * *MyType(double blah) :
> > * * * * *Field1(blah)
> > * * *{
> > * * *}

>
> > * * *bool operator < (const MyType &That) const
> > * * *{
> > * * * * *// Does not use any other member
> > * * * * *return ( fabs(Field1 - That.Field1) > 1e-6 &&
> > * * * * * * * * * Field1 < That.Field1 );
> > * * *}
> > };

>
> > int main()
> > {
> > * * *std::set<MyType> foo;
> > * * *std:air< std::set<MyType>::iterator,
> > * * * * * * * * bool > inchk = foo.insert(MyType(1.0));

>
> > * * *OtherTypes SomeVal = 1;
> > * * *if ( inchk.second )
> > * * * * *inchk.first->MoreFields = SomeVal; // error
> > }

>
> > How do I reassure the compiler that writing MoreFields will not affect
> > any invariants or will not do anything to invalidate the order of
> > elements in the set?

>
> Perhaps you should declare 'MoreFields' "mutable"?
>


Thank you, Victor! I'd forgotten all about that keyword despite that
I'd argued a case for that keyword with a colleague many years ago.


> > If the only recourse is to use another container such as vector, how
> > do I insert a new value in the sorted position while checking if one
> > exists already?

>
> If your vector is sorted, use 'lower_bound' (or is it 'upper_bound'?) to
> find the place where the sorting would place your element, then compare
> what you want to insert to what's already there (the next element or the
> previous, can't divine now) thus /checking/ if one exists.
>


That is more tedious than the alternative of simply declaring other
fields as mutable, IMHO.

thank you again,
- Anand


 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
any chance for contracts and invariants in Python? mrkafk@gmail.com Python 5 02-15-2013 02:33 AM
Class invariants and implicit move constructors (C++0x) Scott Meyers C++ 51 11-20-2010 08:42 PM
Encapsulation and invariants bluekite2000@gmail.com C++ 3 07-29-2005 01:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments