question about std::sort()?

I wrote a class and defined some variables in my program.Then I want to put them to a vector(std::vector<myclass> v1) and sort them with the std::sort().
When I compile it,my compiler,mingw gcc4.3.5,told me that the std::sort() is error! I change my class to int, it works fine!

I have defined the class with operator< and operator ==

Are there someone who can tall me what I should do?

thanks!

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
//my class
class a{
....
public:
a(){...}
a(a&){...}
~a(){...}

....
bool operator<{...}
bool operator=={...}

};

int main(){
a a1,a2... an;
a1 ={...}

a2={...}

an={...}

std::vector<a> v1;
v1.push_back(a1);
{...}
v1.push_back(an);  

std::sort(v1.begin(),v1,end());  //error!!!

{...}

}
After fixing the comma typo on line 28, I was getting a "discards qualifiers" error. They usually have something to do with the const-ness of your object. Adding const to the < operator made it all good. This works:
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
#include <iostream>
#include <vector>
#include <algorithm>

class A
{
    int _a;
public:
    A( int a ) : _a( a ) {}

    int dump() const { return _a; }
    bool operator<( const A & rhs ) const { return _a < rhs._a; }
};

int main()
{
    A a1( 10 );
    A a2( 30 );
    A a3( 20 );

    std::vector<A> v;
    v.push_back(a1);
    v.push_back(a2);
    v.push_back(a3);

    std::sort( v.begin(), v.end() );

    for( std::vector<A>::const_iterator i = v.begin(); i != v.end(); ++i )
    {
        std::cout << (*i).dump() << std::endl;
    }
}


Thanks seymore!

It works fine after adding the "const"!

haaa. So c is more free!
hi, guys!

I have meet another question in gcc sort()!

I built 2 vector<my_class>, and put more than 800 my_class in each of both,then sort them! my program is built by mingw.

my compiler is gcc v4.3.0!above v4.3.5 is wrong!

when I run it, WindowsXP told me a error with message that this program want to write the address 0000000! it works fine with less than 8 my_class.

I checked my program, and found that the error is occurred in the std::sort()!

I copy my codes to Vs2003, and the program work fine!

So my question 1:

is it the problem of gcc?

and I found vs2003 build my program just 300Kb(debug mode,release mode just 100Kb), and gCC is more than1,000KB.

So my question 2:

How to get the optimizing native codes with gcc?
Last edited on
Unlikely to be a gcc bug. Post your code.

Sounds like you are compiling with debug info. Either compile without -g option or run strip -a on the executable (-a option is off the top of my head).
Thanks!

that's my code!

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//head.file

#ifndef BIGN_H_
#define BIGN_H_

#include<string.h>
#include<iostream>
#include<string>
#include<sstream>

std::string int2str(int);

struct Nrep{
    char si;
    std::string name;
    int sz;
    
    Nrep(int n){    ...      }
    
    ~Nrep(){}
private:
    Nrep(const Nrep&);
    Nrep& operator=(const Nrep&);
};

class bign
{		
	Nrep* num;
	int* ref;
public:
	bign():num(new Nrep(0)),ref(new int(1)){}
	bign(int i):num(new Nrep(i)),ref(new int(1)){}
	bign(const bign& b):num(b.num),ref(b.ref){++*b.ref;}
	virtual ~bign();
	
	int size() const{ return num->sz;}
	char operator[](int i) const {if((i<=num->sz)&&(i>0)) return num->name[i-1];else return 0;}
	char getSi() const{ return num->si;}
	
	bign& operator=(const bign&);
	bign& operator+=(const bign&);	
	bign& operator-=(const bign&);
	bign& operator*=(const bign&);
	bool operator==(const bign&) const;
	bool operator>(const bign&)const;
	bool operator<(const bign&) const;
	friend std::ostream& operator<<(std::ostream& oo,const bign& b){if (b.getSi()=='-')return oo<<b.getSi()<<b.num->name;else return oo<<b.num->name;}
	
};

bool big_than(bign& ,bign&);
bool less(bign& ,bign&);
bign& operator* (bign&, const bign&);

#endif /*BIGN_H_*/

//bign.cpp

......

//main cpp

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include"bign.h"

int main(int argc,char* argv[]){

	std::string from;
	
	if(argc!=2)
		from="a1.in";
	else
		from=argv[1];
	
	std::ifstream ii(from.c_str());	
	if(ii==0){
		if(argc <=1)
			std::cout<<"Usage: "<<argv[0]<<" [filename] \n\tPlease try again!\n";
		else
			std::cout<<"Can't open the file: "<<from<<std::endl;
		return -1;
	}
	
	std::string to;
	if(argc>=2)
		to=argv[2];
	else
		to=from;
	
	int n=to.find_last_of('.');
	if (n<1)
		to =to + ".out";
	else
		to.replace(n,from.length(),".out");	
	std::ofstream oo(to.c_str());

	ii>>n;
	for(int loop=0;loop<n;loop++){
		int re1=0;
		int num=0;
		std::vector<bign> v1,v2;
		ii>>num;
		for(int i=0;i<num;i++){
			ii>>re1;
			v1.push_back(re1);
		}
		for(int i=0;i<num;i++){
			ii>>re1;
			v2.push_back(re1);
		}
		
		std::sort(v1.begin(),v1.end());
		std::sort(v2.begin(),v2.end());
		
		bign result;
		for(int i=0;i<num;i++)
			result += v1[i] * v2[num-i-1];
		
		std::cout<<"Case #"<<loop+1<<": "<<result<<std::endl;
		oo<<"Case #"<<loop+1<<": "<<result<<std::endl;
	}
	
	return 0;
	
}

Test file is google's codejam 1A: scalar production!

small test is ok!

big test error!

:(
Topic archived. No new replies allowed.