- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
此处提供类似问题:How do I sort a vector of pairs based on the second element of the pair?但我对外部存储器排序很感兴趣。
我尝试使用内部存储器排序中的类比,但错误发生在 STXXL 的 sorter_stream.h 文件中:
我的代码:
#include <iostream>
#include <stxxl/vector>
#include <stxxl/sorter>
#include <limits>
using namespace std;
typedef std::pair<int,int> my_pair;
struct my_comparator
{
bool operator()(const my_pair& left, const my_pair& right)
{
return left.first < right.first;
}
int min_value() const
{
return std::numeric_limits<int>::min();
}
int max_value() const
{
return std::numeric_limits<int>::max();
}
};
int main()
{
typedef stxxl::sorter<my_pair, my_comparator> sorter_type;
sorter_type int_sorter(my_comparator(), 64 * 1024 * 1024);
for (int i = 10; i > 0; i--)
{
int_sorter.push(my_pair(i,i+10));
}
int_sorter.sort(); // sort elements (in ascending order)
while (!int_sorter.empty())
{
std::cout << (*int_sorter).first << " "<<(*int_sorter).second<<endl;
++int_sorter;
}
return 0;
}
错误:
sort_stream.h(481): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
更新:
将 min_value(),max_value() 函数的返回类型更改为 my_pair 为:
struct my_comparator
{
bool operator()(const my_pair& left, const my_pair& right)
{
return left.first < right.first;
}
my_pair min_value() const
{
return my_pair(std::numeric_limits<int>::min(),std::numeric_limits<int>::min());
}
my_pair max_value() const
{
return my_pair(std::numeric_limits<int>::max(),std::numeric_limits<int>::max());
}
};
出现以下错误:
sort_helper.h(94): error C3848: expression having type 'const my_comparator' would lose some const-volatile qualifiers in order to call 'bool my_comparator::operator ()(const my_pair &,const my_pair &)'
附言: 作为一个新手 (Reputation<50) ,我不被允许发表评论,这就是为什么要写一个新问题。
最佳答案
在 STXXL:Sorter Section 中得到了以下示例,它解决了同样的问题。
代码:
#include <stxxl/sorter>
#include <stxxl/stats>
#include <stxxl/timer>
#include <stxxl/random>
#include <limits>
struct TwoInteger
{
int i, j;
TwoInteger()
{ }
TwoInteger(int _i, int _j)
: i(_i), j(_j)
{ }
};
struct TwoIntegerComparator
{
bool operator () (const TwoInteger& a, const TwoInteger& b) const
{
return a.i < b.i;
}
TwoInteger min_value() const
{
return TwoInteger(std::numeric_limits<int>::min(), std::numeric_limits<int>::min());
}
TwoInteger max_value() const
{
return TwoInteger(std::numeric_limits<int>::max(), std::numeric_limits<int>::max());
}
};
int main()
{
// template parameter <ValueType, CompareType, BlockSize(optional), AllocStr(optional)>
typedef stxxl::sorter<TwoInteger, TwoIntegerComparator, 1*1024*1024> sorter_type;
// create sorter object (CompareType(), MainMemoryLimit)
sorter_type int_sorter(TwoIntegerComparator(), 64 * 1024 * 1024);
stxxl::random_number32 rand32;
stxxl::timer Timer1;
Timer1.start();
// insert random numbers from [0,100000)
for (size_t i = 0; i < 1000; ++i)
{
int_sorter.push(TwoInteger(rand32() % 100000, (int)i)); // fill sorter container
}
Timer1.stop();
STXXL_MSG("push time: " << (Timer1.mseconds() / 1000));
stxxl::timer Timer2;
Timer2.start();
int_sorter.sort(); // switch to output state and sort
Timer2.stop();
STXXL_MSG("sort time: " << (Timer2.mseconds() / 1000));
// echo sorted elements
while (!int_sorter.empty())
{
std::cout << int_sorter->i << " "; // access value
++int_sorter;
}
return 0;
}
关于c++ - STXXL:如何对第二个元素的成对 vector 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691824/
下面的程序,崩溃了 libc++abi.dylib: terminating with uncaught exception of type stxxl::io_error: Error in vir
我想创建一个外部存储器二进制搜索树数据结构,其数据位于使用 stxxl 作为库的外部存储器中。 为此,STXXL 中的哪种数据类型适合用作树中的节点。如果我们使用 stxxl:Vector 作为树的节
我正在开发一个使用 STXXL 的项目,据我所知,它是 C++ STL 库的非核心版本。目前,我的程序运行良好,但我现在面临的问题是,当程序运行时,它使用接近 2GB 的内存(具有中小型数据集)。 在
我正在尝试对包含大约十亿条记录(每条记录包含四个整数)的大文件进行排序。文件的大小将超过 50GB。 我正在用 4 亿条记录(大约 6 GB 文件)测试我的代码。我的磁盘配置如下所示: disk=/v
我有一个需要内置排序的应用程序,我希望用 STXXL 提供的排序替换现有的排序机制。我已经使用 STXXL 成功地测试了它,但我的问题是,虽然特定的排序运行需要对固定长度的字符串进行操作,但长度是在运
我正在尝试使用 STXXL 构建一个大型矩阵,并能够对该矩阵执行多种不同的操作。目前,我正在使用 stxxl::vector 作为存储大矩阵子矩阵的一种方式。我想要做的是规范化矩阵的每一列。所以我唯一
STXXL支持数据处理的流模型。 STXXL 数据可以通过迭代处理(例如使用 stxxl::for_each),或转换为流模式(例如使用 streamify(),其中此类流转换可以组合以进行高效处理。
我需要将大量元素附加到 stxxl vector 中。向 stxxl vector 添加元素的最有效方法是什么?现在,我正在使用 stxxl vector 的 push_back,但它似乎效率不高。它
在stxxl常见问题解答,我发现了这个: Parameterizing STXXL Containers STXXL container types like stxxl::vector can be
以下最小示例说明了在并行初始化容器(使用 openMP)时 stxxl 的行为: #include #include #include typedef stxxl::VECTOR_GENERAT
我似乎无法使用 CMake 2.8.12.1 为 STXXL 构建解决方案。我查看了所有文章并按照他们的说明进行操作,但没有运气。我有带有 SP1 和 Windows 7 的 VS2010 Profe
此处提供类似问题:How do I sort a vector of pairs based on the second element of the pair?但我对外部存储器排序很感兴趣。 我尝试
我正在搜索一个为 Java 中的大型数据集设计的集合框架,它的行为是透明的,比如 STXXL为 C++ 做。 它应该透明地交换到磁盘,但比普通的基于操作系统的 VM 交换更有效。 StringBuff
如何让 qmake 将 stxxl.mk 文件包含到生成的 makefile 中? 我有一个 Qt 项目,它处理大文件 (>RAM),因此想使用 STXXL。 STXXL documentation说
我试图将 STXXL 库安装到 this 之后的自定义路径中以这种方式回答向 cmake 提供前缀: cmake -DCMAKE_INSTALL_PREFIX=/usr 。 && 全部安装 当我运行测
我需要优化 stxxl vector 的 block 大小 BlkSize_ 参数,以便使用简单的网格搜索进行部分和查找。由于为 stxxl vector 指定它的唯一方法似乎是将它用作 vector
我有以下代码,这是一个非常简单的测试,但 VS 拒绝运行它: stxxl::syscall_file OutputFile("Data/test.bin", stxxl::file::RDWR | s
我正在使用 STXXL,有人可以帮我找到编号吗?由我的程序(或算法或进程)完成的 I/O(或 block 传输)?我知道如何限制任何特定进程的内存使用,但不知道如何限制 STXXL 中的 block
这是 Vector of pairs with generic vector and pair type, template of template 的后续. 我希望能够调用带有 std::vecto
我的代码似乎可以工作(由于上述错误,我还没有在大型数据集上尝试过)。 代码: #include #include #include int main() { //queue q; //th
我是一名优秀的程序员,十分优秀!