2012-04-28 14 views
1

私はクラスを作成しましたが、STLアルゴリズムのsort()関数で動作させたいと思います。 コードスニペットをいくつか教えてください。私が作成したクラスにSTL sort()funcを使用する

class bignum_index_cont 
{ 
private: 
mpz_class a; 
long int index; 
public: 
bignum_index_cont() {a=0; index=0;} 
bignum_index_cont(const bignum_index_cont &big) {a=big.a; index=big.index;} 

void bignum_set(mpz_class &c, long int d) {a=c;index=d;} 

bool operator==(bignum_index_cont &big) {return a==big.a;} 
bool operator==(mpz_class &big) {return a==big;} 
bool operator>(bignum_index_cont &big) { return a>big.a;} 
bool operator>=(bignum_index_cont &big) {return a>=big.a;} 
bool operator<(bignum_index_cont &big) {return a<big.a;} 
bool operator<=(bignum_index_cont &big) {return a<=big.a;} 

//some more functions..... that I think will not be needed here. 
}; 

グローバルスペースではvector<bignum_index_cont> hashtx1(pow(2,20)+1);を使用しました。 now、in main()私はいくつかの要素をすべての要素に取り入れることができましたvector<bignum_index_cont> 次に、私はこのベクトルをソートすることにしました。だから、私は、sort(hashtx1.begin(), hashtx1.end()); .. を呼び出したが、私はエラーの巨大なリストを得た。それらは:私はvector<mpz_class> hashtx1(pow(2,20)+1)を取り、ソート()関数を使用している場合、それはうまくコンパイル、

c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h||In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<bignum_index_cont*, std::vector<bignum_index_cont> >, _Tp = bignum_index_cont]':| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h:2253|70|instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<bignum_index_cont*, std::vector<bignum_index_cont> >]'| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h:2284|54|instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<bignum_index_cont*, std::vector<bignum_index_cont> >, _Size = int]'| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h:5407|4|instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<bignum_index_cont*, std::vector<bignum_index_cont> >]'| 
C:\Users\poneer\Desktop\Cryptography\bignum.cpp:121|40|instantiated from here| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h|2212|error: no match for 'operator<' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = bignum_index_cont*, _Container = std::vector<bignum_index_cont>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = bignum_index_cont&]() < __pivot'| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_algo.h|2212|note: candidates are:| 
C:\Users\poneer\Desktop\Cryptography\bignum.cpp|39|note: bool bignum_index_cont::operator<(bignum_index_cont&)| 
C:\Users\poneer\Desktop\Cryptography\bignum.cpp|39|note: no known conversion for argument 1 from 'const bignum_index_cont' to 'bignum_index_cont&'| 
c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_pair.h|207|note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)| 
//+ a lot of errors similar to this 
C:\Files\GMP\GMP\include\gmpxx.h|3150|note: template<class T, class U> bool operator<(long double, const __gmp_expr<T, U>&)| 
//+ a lot similar to this 

注意。 sort()機能で動作するようにコードを改善するにはどうすればよいですか?

もう一つのポイントは、再帰クイックソート()関数を使ってベクトルをソートしようとしたことです。しかし、それは実行中に終了します。

void quicksort(long int left, long int right) 
{ 
long int i=left, j=right; 
bignum_index_cont pivot, temp; 
pivot=hashtx1[(left+right)/2]; 

while(i<=j) 
{ 
    while(hashtx1[i]<pivot) 
     i++; 
    while(hashtx1[j]>pivot) 
     j--; 
    if(i<=j) 
    { 
     temp=hashtx1[i]; 
     hashtx1[i]=hashtx1[j]; 
     hashtx1[j]=temp; 
     i++; j--; 
    } 

} 
quicksort(left, j); 
quicksort(i, right); 
} 

quicksort(0,pow(2,20))を使用して呼びました。

+0

http://stackoverflow.com/questions/2436705/defining-for-stl-sort-algorithm-operator-overload-functor-or-standalone-fun – dirkgently

+0

ありがとうございました。 :) – ponir

答えて

4

あなたは、比較演算子const-referenceへの引数を作成し、演算子const自身をconstにする必要があります。

+0

私はそれをしました。それは、数字の誤りを本当に低くしました。しかし、ここに新しい問題があります エラーは、 'c:\ mingw \ bin \ .. \ lib \ gcc \ mingw32 \ 4.6.2 \ include \ C++ \ bits \ stl_algo.h | 2215 |エラーです。 : 'bool bignum_index_cont :: operator {(const bignum_index_cont&)'の 'this'引数に 'const bignum_index_cont'を渡すと修飾子が破棄されます。[0123]離れて。ありがとう。 :) 今、私は私がしたいことをすることができるかどうかを確認する必要があります。 :) – ponir

+0

@ user817824演算子<演算子<(const bignum_index_cont&big)const'(または自由関数または静的関数)として定義する必要があります。他の人と同じです。ちなみに – sepp2k

+0

、私はそれもconstにしなければならない理由を説明できますか? [私が学んだ本はこれに関する情報を持っていません。] – ponir

関連する問題