是否有可能random_shuffle INT元素的数组?有可能、数组、元素、INT

2023-09-11 02:23:23 作者:千般好也敌不过一次错

我读了这一点: http://www.cplusplus.com/reference/算法/ random_shuffle / 并想知道如果可能的random_shuffle INT元素的数组。这是我的code

 的#include<的iostream>
#包括<算法>

使用名字空间std;

诠释的main()
{
    诠释一个[10] = {1,2,3,4,5,6,7,8,9,10};

    COUT<< A<< ENDL<< ENDL;

    random_shuffle(一个[0],A [9]);

    的cout&其中;&其中;一个;
}
 

我得到这个错误:

 错误C2893:无法专注函数模板
    iterator_traits实现< _Iter> :: difference_type *的std :: _ Dist_type(_Iter)。
 

我的问题是:

是否有可能使用 random_shuffle 洗牌的int数组。如果是的话,我想学习如何做到这一点。

random_shuffle 只适用于模板?

什么是我的错误是什么意思?

解决方案 matlab怎么构造一个元素是数组的向量

您需要传递指针 A [0] A [10 ] ,而不是元素本身:

  random_shuffle(安培; A [0],和放大器;一个[10]); //端必须是10,而不是9
 

在C ++ 11,你可以用的std ::开始的std ::结束

  random_shuffle(STD ::开始(一),的std ::结束(一));
 

I was reading up on this : http://www.cplusplus.com/reference/algorithm/random_shuffle/ and wondered if its possible to random_shuffle an array of int elements. This is my code

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a[10]={1,2,3,4,5,6,7,8,9,10};

    cout << a << endl << endl;

    random_shuffle(a[0],a[9]);

    cout<<a;
}

I got this error:

error C2893: Failed to specialize function template
    'iterator_traits<_Iter>::difference_type *std::_Dist_type(_Iter)'.

My question are:

Is it possible to shuffle an int array using random_shuffle. If yes, I would like to learn how to do it.

Is random_shuffle only applicable to templates?

What does my error mean?

解决方案

You need to pass pointers to a[0] and a[10], not the elements themselves:

random_shuffle(&a[0], &a[10]); // end must be 10, not 9

In C++11, you can use std::begin and std::end:

random_shuffle(std::begin(a), std::end(a));