473,587 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(vec torSize,set<myE lement>());
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(myEleme nt);
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 2190


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(vec torSize,set<myE lement>());
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(myEleme nt);
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******@gasca d.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(vec torSize,set<myE lement>());
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(myEleme nt);
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(vec torSize,set<myE lement>());
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<myElem ent>::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.dfn cis.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<sequencedTi me> >
sequencedTimes( runwayNum,set<s equencedTime>() );
int * times = new int[indivSize];
for(int i=0; i<indivSize; i++) {
int earliest = (_ptrFleet->GetAircraftWit hIndex(i))->GetEarliestTim e();
int latest = (_ptrFleet->GetAircraftWit hIndex(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<sequen cedTime>::itera tor,bool> result =
(sequencedTimes[runwayLabel-1]).insert(seqTim e);
}

'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(ru nwayNum,vector< int>());
int * runways = new int[indivSize];
for(int i=0;i<indivSize ;i++) {
int planeLabel = tmpSeq[i];
int runwayLabel = (rand()%runwayN um)+1;
runways[planeLabel-1] = runwayLabel;
tmpSequences[runwayLabel-1].push_back(plan eLabel);
}

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
2646
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 with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid...
3
2930
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 instantiate std::fill<> or cast 0 to XXX*.
9
13976
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 encountered on device "TBS_IDX". Do you want to continue(c), terminate this device only(d), abort the utility(t) ? (c/d/t) t
8
2903
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"> <HeaderTemplate><table> </HeaderTemplate> <ItemTemplate>
2
1354
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 table. How do I fill my dataset when the sp is a combination of two tables. i'm not able to understand how to fill it when i'm using a sql server...
11
3033
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
3255
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 to the function in the container. I allocate (and free in c) arrays to get garbage collection in both C and C#. After a few seconds I get an...
4
1797
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 class="containers"> <div class="ml"></div> <div class="mm">1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br />1<br...
2
8590
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 USING S ON Z ON MYDB_NODE0000.out V ON DB20000I The UPDATE COMMAND OPTIONS command completed successfully. SET CLIENT ATTACH_DBPARTITIONNUM 0...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.