stl issue, comments

V

Vladimir Ralev

Bad stuff happens lately. I almost skrewed it up on a programming
contest yesterday. I tried few runs with this code (testing), it
behaved well, until test 59(or something). Means - doing pretty well
... But then - (on gcc) i get segmentation failure. Turned on stl
debugging symbols (on VC)...and i got something called DAMAGE ERROR
after normal block #57. After 40 minutes of mad code scramble i
managed to get it str8, but i still cant figure out what is that
error. I can only say it has something to do with memory allocation
checks in debug stl builds.
Thanks for any comments on that! It almost got me and costed me 40
minutes for such a simple program.

#include<vector>
#include<algorithm>
using namespace std;
struct offbandp
{
int offnum;
int closerto;
bool biggre;
};
bool operator< (const offbandp &s1,const offbandp &s2)
{
if(s1.closerto==s2.closerto) return s1.biggre;//bug here?note biggre
*is* bool
//fix:if(s1.closerto==s2.closerto) return s1.biggre>s2.biggre;
return s1.closerto<s2.closerto;
}
class SkipRope
{
public:
vector <int> partners(vector <int> candidates, int height)
{
int q=0;
vector<offbandp> vecstr(candidates.size());
for(q=0;q<vecstr.size();q++)
{
vecstr[q].closerto=abs(height-candidates[q]);
vecstr[q].offnum=q;
vecstr[q].biggre=int(height<candidates[q]);
}
sort(vecstr.begin(),vecstr.end()); <- DAMAGE ERROR here
vector<int> res(2);
int a=vecstr[0].offnum,b=vecstr[1].offnum;
res[0]=candidates[vecstr[0].offnum];
res[1]=candidates[vecstr[1].offnum];
sort(res.begin(),res.end());
return res;
}
};
void main()
{
int test[]={82, 105, 112, 147, 112, 77, 93, 127, 158, 95, 151, 139,
103, 103, 174, 112, 95, 108, 109, 162, 164, 118, 119, 121, 156, 150,
169, 127, 171, 102, 123, 149, 132, 108, 83, 148, 174};
vector<int> test2(test,test+sizeof(test)/sizeof(int));
SkipRope t;t.partners(test2,99);
}
 
T

tom_usenet

Bad stuff happens lately. I almost skrewed it up on a programming
contest yesterday. I tried few runs with this code (testing), it
behaved well, until test 59(or something). Means - doing pretty well
.. But then - (on gcc) i get segmentation failure. Turned on stl
debugging symbols (on VC)...and i got something called DAMAGE ERROR
after normal block #57. After 40 minutes of mad code scramble i
managed to get it str8, but i still cant figure out what is that
error. I can only say it has something to do with memory allocation
checks in debug stl builds.
Thanks for any comments on that! It almost got me and costed me 40
minutes for such a simple program.

#include<vector>
#include<algorithm>
using namespace std;
struct offbandp
{
int offnum;
int closerto;
bool biggre;
};
bool operator< (const offbandp &s1,const offbandp &s2)
{
if(s1.closerto==s2.closerto) return s1.biggre;//bug here?note biggre
*is* bool
//fix:if(s1.closerto==s2.closerto) return s1.biggre>s2.biggre;
return s1.closerto<s2.closerto;

Operator< must implement a strict weak ordering to be compatible with
std::sort (and also to be compatible with conventional ideas of <).
With yours, imagine:

A: closerto=10, biggre=true
B: closerto=10, biggre=true

That gives A < B && B < A, which isn't possible in a strict weak
ordering. What you wanted to return was A == B, in other words, !(A <
B) and !(B < A). Sort is only as good as the comparator you pass it.

Tom
 
M

Mike Wahler

Vladimir Ralev said:
Bad stuff happens lately. I almost skrewed it up on a programming
contest yesterday. I tried few runs with this code (testing), it
[snip]

void main()

Programming contest? You lose.

-Mike
 
V

Vladimir Ralev

First, thanks to tom.

Mike Wahler said:
Vladimir Ralev said:
Bad stuff happens lately. I almost skrewed it up on a programming
contest yesterday. I tried few runs with this code (testing), it
[snip]

void main()

Programming contest? You lose.

heh, i actually did better than most guys in my room....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,380
Latest member
AlinaBlevi

Latest Threads

Top