By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

stl issue, comments

P: n/a
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);
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 21 Nov 2003 07:16:41 -0800, v_*****@yahoo.com (Vladimir Ralev)
wrote:
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
Jul 22 '05 #2

P: n/a
"Vladimir Ralev" <v_*****@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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
Jul 22 '05 #3

P: n/a
First, thanks to tom.

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<TG*****************@newsread1.news.pas.earth link.net>...
"Vladimir Ralev" <v_*****@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
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....
-Mike

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.