Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用指针实现配分函数中的随机配分点_C++ - Fatal编程技术网

C++ 用指针实现配分函数中的随机配分点

C++ 用指针实现配分函数中的随机配分点,c++,C++,我试图将下面显示的分区函数从选择最后一个元素作为分区转换为随机元素。我最初的想法是使用rand函数 int *pivot = rand() % last; 然而,我意识到,试图将整数解析为指针整数是行不通的。我有没有办法回避这个问题 int *partition(int *first, int *last) { int *pivot = last - 1; int *i = first; int *j = last - 1; for (;;) {

我试图将下面显示的分区函数从选择最后一个元素作为分区转换为随机元素。我最初的想法是使用rand函数

int *pivot = rand() % last;
然而,我意识到,试图将整数解析为指针整数是行不通的。我有没有办法回避这个问题

int *partition(int *first, int *last)
{

    int *pivot = last - 1;
    int *i = first;
    int *j = last - 1;
    for (;;)
    {
        while (comp_less(*i, *pivot) && i < last)
        {
            ++i;
        }
        while (*j >= *pivot && j > first)
        {
            --j;
        }
        if (i >= j)
            break;

        swap(*i, *j);
    }
    swap(*(last - 1), *i);
    return i;
}
int*分区(int*first,int*last)
{
int*pivot=last-1;
int*i=第一;
int*j=last-1;
对于(;;)
{
而(比较少(*i,*pivot)和&i=*轴和&j>第一个)
{
--j;
}
如果(i>=j)
打破
互换(*i,*j);
}
掉期(*(最后-1),*i);
返回i;
}
您需要在[0,
std::distance(first,last)
范围内选择一个距离,然后将其添加到
first

请注意,
rand
生成低质量的伪随机数,
rand()%N
不会统一拾取数字

int * random_pivot(int * first, int * last)
{
    thread_local std::mt19937 random_engine(std::random_device{}());

    std::uniform_int_distribution<std::ptrdiff_t> range(0, std::distance(first, last) - 1);
    return first + range(random_engine);
}
int*random_pivot(int*first,int*last)
{
线程本地std::mt19937随机引擎(std::随机设备{}());
标准:均匀分布范围(0,标准:距离(第一,最后)-1);
返回第一个+范围(随机_引擎);
}
如果您在其他地方使用随机选择,最好通过引用传入
random\u engine
,并且只初始化一个(每个线程)
std::mt19937
,您需要在[0,
std::distance(first,last)
范围内选择一个距离,然后将其添加到
first

请注意,
rand
生成低质量的伪随机数,
rand()%N
不会统一拾取数字

int * random_pivot(int * first, int * last)
{
    thread_local std::mt19937 random_engine(std::random_device{}());

    std::uniform_int_distribution<std::ptrdiff_t> range(0, std::distance(first, last) - 1);
    return first + range(random_engine);
}
int*random_pivot(int*first,int*last)
{
线程本地std::mt19937随机引擎(std::随机设备{}());
标准:均匀分布范围(0,标准:距离(第一,最后)-1);
返回第一个+范围(随机_引擎);
}
如果您在其他地方使用随机选择,最好通过引用传入
random\u engine
,并且只初始化一个(每个线程)
std::mt19937