使用 STL sort() func 用于我构建的类
Using STL sort() func for a class I built
我写了一个类,我希望它与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>
的所有元素进行输入然后我决定对这个向量进行排序。所以,我打电话给,sort(hashtx1.begin(), hashtx1.end());
..但我得到了大量的错误列表。它们是:
c:mingwbin..libgccmingw324.6.2includec++bitsstl_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:mingwbin..libgccmingw324.6.2includec++bitsstl_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:mingwbin..libgccmingw324.6.2includec++bitsstl_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:mingwbin..libgccmingw324.6.2includec++bitsstl_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:UsersponeerDesktopCryptographybignum.cpp:121|40|instantiated from here|
c:mingwbin..libgccmingw324.6.2includec++bitsstl_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:mingwbin..libgccmingw324.6.2includec++bitsstl_algo.h|2212|note: candidates are:|
C:UsersponeerDesktopCryptographybignum.cpp|39|note: bool bignum_index_cont::operator<(bignum_index_cont&)|
C:UsersponeerDesktopCryptographybignum.cpp|39|note: no known conversion for argument 1 from 'const bignum_index_cont' to 'bignum_index_cont&'|
c:mingwbin..libgccmingw324.6.2includec++bitsstl_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:FilesGMPGMPincludegmpxx.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() 函数对向量进行排序。但它在运行时终止。
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))
来称呼它。
您需要对
比较运算符进行参数 const-引用,并使运算符自己构成。
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- meson-build:wxWidgets 资源文件,用于 Windows 未构建
- 用于构建 cuda .so 文件(共享库)的生成文件
- 用于自定义调试构建的 CMake qt 输入库后缀
- C++查找用于构建单行计算器
- 使用VS2017构建用于Linux的Protobuf
- 构建共享对象 - 使用 Ninja 编译适用于 OSX 和 Windows 上的 Ubuntu Crush
- MoltenVK(适用于 Vulkan)构建在 OS X v. 10.13.3 (17D102) 上失败 |Xcode
- 在构建之前,如何使Cmake在构建之前运行Python脚本,以生成用于构建中的项目的文件
- 用于构建 POCO 库的 mysql 连接器
- 构建适用于 iOS 的 ICU
- Cmake:如何构建自定义编译器二进制文件,然后将其用于某些目标?
- mac构建有一个未定义的符号,而linux构建并没有用于相同的代码
- 在霍夫曼压缩后将代码表存储在压缩文件中,并从此表构建用于解压缩的树
- 同时构建用于提升的32位和64位库
- 用g++构建用于MSVC应用程序的dll
- 正在构建用于Oracle jvm的avahi(错误:jni.h未找到)
- 在Xcode 4中构建用于Objective-c应用程序的静态c++库
- 正在构建用于编译boost的Openssl源代码