gpt4 book ai didi

c++ - 对我构建的类使用 STL sort() func

转载 作者:行者123 更新时间:2023-11-30 02:04:39 24 4
gpt4 key购买 nike

我写了一个类,我希望它与 STL 算法 sort() 函数一起工作。让我给出一些代码片段;

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);在全局空间。现在,在 main()我知道如何设法对 vector<bignum_index_cont> 的所有元素进行输入然后我决定对这个 vector 进行排序。所以,我调用sort(hashtx1.begin(), hashtx1.end()); ..但我得到了一大堆错误。它们是:

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

请注意,如果我取 vector<mpz_class> hashtx1(pow(2,20)+1)并使用 sort() 函数,它编译得很好。我怎样才能改进我的代码,使其与 sort() 一起工作?功能?

另一点是,我什至尝试过使用递归 quicksort() 函数对 vector 进行排序。但它在运行时终止。

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)) 调用它.

最佳答案

您需要使比较运算符的参数成为常量引用,并使运算符本身成为常量。

关于c++ - 对我构建的类使用 STL sort() func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com