Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem to fill a combination of containers

Reply
Thread Tools

problem to fill a combination of containers

 
 
Helene Pinol
Guest
Posts: n/a
 
      06-26-2003
Dear All

I have a problem to insert elements in a combination of containers.
Here is the declaration of the container I would like to use:
vector< set<myElement> > myContainer(vectorSize,set<myElement>());
The size of the vector is known but not yet the sizes of the different
sets.
Within a for loop, I build some myElements and try to insert them in
the correct set with:
myContainer[num].insert(myElement);
num is strictly less than vectorSize
myElement is defined as a struct of 2 int and overloads the operator<
in order to do the insertion
With a vectorSize equals to 1, the first insertion is done OK but it
fails within the second insertion; with a vectorSize>1 sometimes even
the first insertion fails.

What I don't understand is that I've done something similar that works
with a vector of vector of int and using a push_back instead of the
insert.

Thanks for your help.

Hélène.
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-26-2003


Helene Pinol wrote:
>
> Dear All
>
> I have a problem to insert elements in a combination of containers.
> Here is the declaration of the container I would like to use:
> vector< set<myElement> > myContainer(vectorSize,set<myElement>());
> The size of the vector is known but not yet the sizes of the different
> sets.
> Within a for loop, I build some myElements and try to insert them in
> the correct set with:
> myContainer[num].insert(myElement);
> num is strictly less than vectorSize
> myElement is defined as a struct of 2 int and overloads the operator<
> in order to do the insertion
> With a vectorSize equals to 1, the first insertion is done OK but it
> fails within the second insertion; with a vectorSize>1 sometimes even
> the first insertion fails.
>
> What I don't understand is that I've done something similar that works
> with a vector of vector of int and using a push_back instead of the
> insert.


Please post real code instead of english descriptions.

--
Karl Heinz Buchegger

 
Reply With Quote
 
 
 
 
Helene Pinol
Guest
Posts: n/a
 
      06-26-2003
Karl Heinz Buchegger <> wrote in message news:<>...
> Helene Pinol wrote:
> >
> > Dear All
> >
> > I have a problem to insert elements in a combination of containers.
> > Here is the declaration of the container I would like to use:
> > vector< set<myElement> > myContainer(vectorSize,set<myElement>());
> > The size of the vector is known but not yet the sizes of the different
> > sets.
> > Within a for loop, I build some myElements and try to insert them in
> > the correct set with:
> > myContainer[num].insert(myElement);
> > num is strictly less than vectorSize
> > myElement is defined as a struct of 2 int and overloads the operator<
> > in order to do the insertion
> > With a vectorSize equals to 1, the first insertion is done OK but it
> > fails within the second insertion; with a vectorSize>1 sometimes even
> > the first insertion fails.
> >
> > What I don't understand is that I've done something similar that works
> > with a vector of vector of int and using a push_back instead of the
> > insert.

>
> Please post real code instead of english descriptions.


Here is how myElement is defined:
struct myElement {
int x1;
int x2;
public:
bool operator<(const myElement & myElt) const {
return (x2 <= myElt.x2);
}
};

Here is the declaration of the container and how I try to use it:
vector< set<myElement> > myContainer(vectorSize,set<myElement>());
for(int i=0; i<maxNum; i++) {
// some computations to get 'val' & 'num'
// some printings to check values of 'val' & 'num'
myElement myElt = {i+1,val};
pair<set<myElement>::iterator,bool> result =
myContainer[num].insert(myElt);
}

With 'vectorSize'=1, 'num' is equal to 0 as it should be. A first
'myElt' element can be inserted. For the following one, the program
crashes on the insertion instruction (not possible to check the values
of the 'result' pair).

Is it clearer? Do you need to know something else? Do you have any
idea about what I've done wrong?

Thanks for your help.

Helene.
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-26-2003
> >
> > Please post real code instead of english descriptions.

>
> Here is how myElement is defined:
> struct myElement {
> int x1;
> int x2;
> public:
> bool operator<(const myElement & myElt) const {
> return (x2 <= myElt.x2);
> }
> };


You certainly have a problem here. With this defintion of operator< it is
possible that both a < b and b < a could be true (if a.x2 == b.x2). That
could certainly be enough to crash the insert operation.

john


 
Reply With Quote
 
Helene Pinol
Guest
Posts: n/a
 
      06-27-2003
"John Harrison" <> wrote in message news:<bdfc93$qj714$>...
> > >
> > > Please post real code instead of english descriptions.

> >
> > Here is how myElement is defined:
> > struct myElement {
> > int x1;
> > int x2;
> > public:
> > bool operator<(const myElement & myElt) const {
> > return (x2 <= myElt.x2);
> > }
> > };

>
> You certainly have a problem here. With this defintion of operator< it is
> possible that both a < b and b < a could be true (if a.x2 == b.x2). That
> could certainly be enough to crash the insert operation.
>
> john


Here is my actual code. I hope everything relevant is here. Otherwise
do not hesitate to ask me for more details.

vector< set<sequencedTime> >
sequencedTimes(runwayNum,set<sequencedTime>());
int * times = new int[indivSize];
for(int i=0; i<indivSize; i++) {
int earliest = (_ptrFleet->GetAircraftWithIndex(i))->GetEarliestTime();
int latest = (_ptrFleet->GetAircraftWithIndex(i))->GetLatestTime();
int time = earliest + (indiv->_proportions)[i] *
(latest-earliest);
times[i] = time;
sequencedTime seqTime = {i+1,time};
int runwayLabel = (indiv->_runways)[i];
pair<set<sequencedTime>::iterator,bool> result =
(sequencedTimes[runwayLabel-1]).insert(seqTime);
}

'runwayNum' is equal to 1
'indivSize' is equal to 10
They are arguments given to the program.
'earliest' and 'latest' are also data input to the program.
The arrays '_runways' and '_proportions' are of size 10 ('indivSize')
and contain the following values before entering the 'for' loop.
[runways] 1 1 1 1 1 1 1 1 1 1
[(aircraft)/proportion] (1)/0.074 (2)/0.000 (3)/0.057 (4)/0.033
(5)/0.031 (6)/0
..075 (7)/0.033 (/0.086 (9)/0.079 (10)/0.051
In the last execution, the first 'seqTime' is {1,160} and is inserted
ok and the second is {2,195} and makes the program crashes.

The 'sequencedTime' element is entirely defined as following:
struct sequencedTime {
int plane;
int time;
public:
bool operator<(const sequencedTime & st) const {
return (time <= st.time);
}
};
Using '<' or '<=' does not make a difference in the program's
behavior. That is it crashes at the second insertion with a 'cannot
read the memory' message.

I've done something pretty similar earlier in my program that works
with a vector of vector of int. Here is the code for this case:

vector< vector<int> > tmpSequences(runwayNum,vector<int>());
int * runways = new int[indivSize];
for(int i=0;i<indivSize;i++) {
int planeLabel = tmpSeq[i];
int runwayLabel = (rand()%runwayNum)+1;
runways[planeLabel-1] = runwayLabel;
tmpSequences[runwayLabel-1].push_back(planeLabel);
}

Again 'runwayNum' is equal to 1 and 'indivSize' to 10 and 'tmpSeq' is
an array of length 10 ('indivSize') that can be such as 3 4 5 1 7 9 6
2 10 8

Thanks to all of you for your help.

Hélène.
 
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
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
problem about combination gridview-dropdownlist (and selectedvalue) Ben ASP .Net 0 08-15-2006 04:22 PM
std::fill and containers of pointers red floyd C++ 3 08-06-2004 08:55 PM
char* combination problem :( x C++ 9 04-14-2004 01:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57