473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem to fill a combination of containers

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.
Jul 19 '05 #1
4 2180


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
kb******@gascad.at
Jul 19 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3E***************@gascad.at>...
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.
Jul 19 '05 #3
> >
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
Jul 19 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bd************@ID-196037.news.dfncis.de>...

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 (8)/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.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
3
by: red floyd | last post by:
I got an error by using std::fill to set an array of pointers to 0. e.g.: class XXX; XXX* v; std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type I have to either explicitly...
9
by: GL | last post by:
I am running DB2 8.1.1 on AIX 5.1 Having a problem with a redirected restore. Once into the restore continue phase, I immediately get the following “SQL2059W A device full warning was...
8
by: I am Sam | last post by:
Hi everyone, This problem is making me old. I don't want to get any older. I have a multi-nested repeater control as follows: <asp:Repeater ID="clubRep1" Runat="server">...
2
by: savvy | last post by:
I'm trying to page a datagrid but the when i'm tying to fill the DataSet with a table, I know that using DataAdapter.Fill you can specify a single datatable to populate when the sp returns one...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
6
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls...
4
by: woody79 | last post by:
My HTML: <div style="position:relative;"><div id="mainbody"> <div class="containers"> <div class="tl"></div> <div class="tm">Articles</div> <div class="tr"></div> </div> <div...
2
by: modeler | last post by:
Hello, I am trying to restore a DB2 9.1 fp2 backup to another DB2 9.1 fp2 machine, both are on Windows. C:\Program Files\IBM\SQLLIB\BIN>db2 -tvf C:\db2_data\test.db2 UPDATE COMMAND OPTIONS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.