C++ 调用并打印返回c+中数组的函数+;

C++ 调用并打印返回c+中数组的函数+;,c++,C++,我有一个函数,它在c++中返回一个随机数组: int* randomArray(int countOfRows){ int test1 [countOfRows] = {}; int insertValue; int check; for (int n=0; n < countOfRows; ++n){ srand(time (NULL) ); while (test1[n] == NULL){

我有一个函数,它在c++中返回一个随机数组:

int* randomArray(int countOfRows){
    int test1 [countOfRows] = {};
    int insertValue;
    int check;
        for (int n=0; n < countOfRows; ++n){
            srand(time (NULL) );
            while (test1[n] == NULL){
                insertValue = (rand () %100 + 1 );
                for(int i = 0; i < countOfRows; i++){
                    if (test1[i] == insertValue){
                        check = 1;
                        break;
                    }
                    else{
                        check = 0;
                    }
                }

                if (check == 0){
                    test1[n] = insertValue;
                }
            }
        }
    return test1;
}
int*随机数组(int countOfRows){
int test1[countOfRows]={};
int-insertValue;
整数检查;
for(int n=0;n
  • 我怎样才能称之为数组
  • int*和int[]之间的区别是什么

谢谢:)

在我看来,这个函数有问题

它返回test1的点,该点在堆栈中分配,在randomArray范围之外无效

int *test1 = (int*) malloc(countOfRows* sizeof(int));
所以,如果您更改为malloc,这是在堆中分配的,那么它在超出randomArray范围时仍然有效

int *test1 = (int*) malloc(countOfRows* sizeof(int));
您可以使用test1[x]获取每个int的值,当然您应该知道test1的长度是countOfRows

请不要忘记在未使用时删除此点

调用这个数组很简单

int*值=随机数组(1000);

printf(“%d\r\n”,值[0])

在我看来,此函数存在问题

它返回test1的点,该点在堆栈中分配,在randomArray范围之外无效

int *test1 = (int*) malloc(countOfRows* sizeof(int));
所以,如果您更改为malloc,这是在堆中分配的,那么它在超出randomArray范围时仍然有效

int *test1 = (int*) malloc(countOfRows* sizeof(int));
您可以使用test1[x]获取每个int的值,当然您应该知道test1的长度是countOfRows

请不要忘记在未使用时删除此点

调用这个数组很简单

int*值=随机数组(1000);

printf(“%d\r\n”,值[0])

在函数randomArray()中将test1[]声明为静态int[]。 使用指针返回数组, “返回test1” 在main函数中,使用指针访问返回值
函数randomArray()中的“int*ptr=randomArray(n)”

将test1[]声明为静态int[]。 使用指针返回数组, “返回test1” 在main函数中,使用指针访问返回值
“int*ptr=randomArray(n)”

您的代码有四个重要问题,其中一个是关键问题,一个是非标准和依赖于实现的问题,还有两个是一般算法问题

首先,最重要的是,您将返回一个自动变量的地址,这意味着它既没有用处,也将调用未定义的行为来取消调用方的引用。在函数顶部声明的是:

int test1 [countOfRows] = {};

它本身带来第二点,这个非标准有两个原因:可变长度数组不被C++标准支持,并且通过推断,同样地不支持该初始化。后来

return test1;
函数的调用者将收到一个地址,但该地址无效。它不再解决任何具体的问题,因为函数返回后,
test1
不再存在。这是通过多种方式来补救的,并且考虑到这是C++,最简单的是使用<代码> STD::vector < /代码>,它支持值返回。 两个重要的算法问题是

  • srand
    的种子设定不应在for循环中。事实上,如果您使用的是
    srand
    rand
    ,那么在整个过程中,种子设定应该只执行一次
  • 如果您只是使用不同的算法(稍后我将介绍),则无需进行穷举搜索以查看当前随机选取是否已用于避免重复
因此,最简单的代码修复方法是:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

std::vector<int> randomArray(int countOfRows)
{
    std::vector<int> test1(countOfRows);
    int check = 0;
    for (int n=0; n < countOfRows; ++n)
    {
        while (test1[n] == 0)
        {
            int insertValue = (rand () %100 + 1 );
            for(int i = 0; i < countOfRows; i++)
            {
                if (test1[i] == insertValue){
                    check = 1;
                    break;
                }
                else{
                    check = 0;
                }
            }

            if (check == 0){
                test1[n] = insertValue;
            }
        }
    }
    return test1;
}

int main()
{
    std::srand(static_cast<unsigned>(std::time(NULL)));
    std::vector<int> vec = randomArray(20);
    for (auto x : vec)
        std::cout << x << ' ';
    std::cout.put('\n');
}

有限集算法

这里真正要生成的是一组有限的整数,范围为1..100。也就是说,没有使用重复的值,并且返回的项目数也可以是1到100之间的任何值。要做到这一点,考虑这个算法:

  • std::vector
  • 使用标准库中的伪随机生成器,使用
    std::shuffle
  • 将结果向量调整为要返回的元素数
  • 关于上面的3,考虑一个小例子,假设你只需要十个元素。最初构建的序列向量如下所示:

    1 2 3 4 5 6 7 8 9 10 11 12 13...  ...99 100
    
    现在,您可以使用
    std::shuffle
    和伪随机生成器(如
    std::mt19937
    ):(为了简洁起见,显示了前二十个元素):

    现在,只需将向量调整为所需大小,在本例中为十个元素:

    48 39 31 44 68 84 98 40 57
    
    这就是你的结果。如果这听起来很复杂,您可能会惊讶地发现它实际上只需要很少的代码:

    代码

    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <numeric>
    #include <random>
    
    std::vector<int> randomSequence100(std::size_t count)
    {
        if (count > 100)
            count = 100;
    
        static std::random_device rd;
        std::vector<int> result(100);
        std::iota(result.begin(), result.end(), 1);
        std::shuffle(result.begin(), result.end(), std::mt19937(rd()));
        result.resize(count);
    
        return result;
    }
    
    
    int main()
    {
        // run twenty tests of random shuffles.
        for (int i=0; i<20; ++i)
        {
            auto res = randomSequence100(20);
            for (auto x : res)
                std::cout << x << ' ';
            std::cout.put('\n');
        }
    }
    
    上面的每一行是从1..100的序列中提取的一组20个元素。没有一行有重复项(如果需要,请检查)

    警告

    27 71 58 6 74 65 56 37 53 44 25 91 10 86 51 75 31 79 18 46 
    6 61 92 74 30 20 91 89 64 55 19 12 28 13 5 80 62 71 29 43 
    92 42 2 1 78 89 65 39 37 64 96 20 62 33 6 12 85 34 29 19 
    46 63 8 44 42 80 70 2 68 56 86 84 45 85 91 33 20 83 16 93 
    100 99 4 20 47 32 58 57 11 35 39 43 87 55 77 51 80 7 46 83 
    48 39 31 44 68 84 98 40 57 76 70 16 30 93 9 51 63 65 45 81 
    32 73 97 83 56 49 39 29 3 59 45 89 43 78 61 5 57 51 82 8 
    21 46 25 29 48 37 77 74 32 56 87 91 94 86 57 67 33 9 23 36 
    27 46 66 40 1 72 41 64 53 26 31 77 42 38 81 47 58 73 4 11 
    79 77 46 48 70 82 62 87 8 97 51 99 53 43 47 91 98 81 64 26 
    27 55 28 12 49 5 70 94 77 29 84 23 52 3 25 56 18 45 74 48 
    95 33 25 80 81 53 55 11 70 2 38 77 65 13 27 48 40 57 87 93 
    70 95 66 84 15 87 94 43 73 1 13 89 44 96 10 58 39 2 23 72 
    43 53 93 7 95 6 19 89 37 71 26 4 17 39 30 79 54 44 60 98 
    63 26 92 64 83 84 30 19 12 71 95 4 81 18 42 38 87 45 62 70 
    78 80 95 64 71 17 14 57 54 37 51 26 12 16 56 6 98 45 92 85 
    89 73 2 15 43 65 21 55 14 27 67 31 54 52 25 72 41 6 85 33 
    4 87 19 95 78 97 27 13 15 49 3 17 47 10 84 48 37 2 94 81 
    15 98 77 64 99 68 34 79 95 48 49 4 59 32 17 24 36 53 75 56 
    78 46 20 30 29 35 87 53 84 61 65 85 54 94 68 75 43 91 95 52 
    
    这项技术对于小型域或大型域的大型结果集都非常有效。但它有其局限性。p> 例如:一旦您的潜在域达到有效大小(例如,1…1000000中的数字),并且您只需要小的结果集(例如,不超过100个元素),您最好使用
    std::unordered_set
    和类似于您现在所做的迭代探测。您使用的技术完全取决于您的性能目标和使用模式

    反例:如果你想从中洗牌50万个独特的元素