使用向量而不是数组

Using Vectors instead of Arrays

我写了一个生成唯一随机数的小程序。我首先使用我所知道的数组编写它来加载和打印数字。我正在尝试用向量替换数组,所以如果我想复制列表,我可以更轻松地做到这一点。我收到一个错误。

  error: cannot convert 'std::vector<int>' to "std::vector<int>*' for argument '1' to bool numInList(std::vector<int>*, int)' 

当我调用 numInList 函数时出现此错误。

我是使用向量的新手,但我认为您可以像数组一样使用向量,它具有内置函数、没有固定大小以及将一个向量复制到另一个向量的能力等优点。

这是我的代码:

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


using namespace std;
bool numInList(vector <int> randNumbers[], int num);

const int length = 100;

int main()
{
int countCheck = 0;
vector <int> randNumbers(length);
vector <int> newlist();

srand(time(0));

 while(countCheck < length){
    int num = rand() % 90000 +10000;

    if (!numInList(randNumbers, num)){
        randNumbers[countCheck] = num;
        cout << "The Random Number  " << randNumbers[countCheck] << endl;
        countCheck++;
    }
 }

cout << "\n\n\n";
newlist[] = randNumbers[];

return 0;
}
bool numInList(vector<int> randNumbers[], int num){
for (int index = 0; index < length; index++){
    if (randNumbers[index] == num){
        return true;
    }
}
return false;
}

我尝试取消引用希望能解决问题

  if (!numInList(&randNumbers, num))

然后我在函数 numInList

中的 IF 语句中得到一个错误
 error: ISO C++ forbids comparisons between pointers and integer [f-permissive]  

如有任何帮助,我们将不胜感激。


我更改了一些东西,现在我没有遇到任何编译错误,但是程序在执行时崩溃了...有什么建议吗???

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


    using namespace std;
    bool numInList(vector <int> randNumbers, int num);

    const int length = 100;

    int main()
    {
    int countCheck = 0;
    vector <int> randNumbers;
    vector <int> newlist;

    srand(time(0));

    while(countCheck < length){
        int num = rand() % 90000 +10000;

        if (!numInList(randNumbers, num)){
            randNumbers.push_back(num);
            cout << "The Random Number  " << randNumbers[countCheck] << endl;
            countCheck++;
        }
     }

     cout << "\n\n\n";
     newlist = randNumbers;

     return 0;
     }
    bool numInList(vector<int> randNumbers, int num){
    for (int index = 0; index < length; index++){
        if (randNumbers[index] == num){
            return true;
        }
    }
    return false;
   }

numInList 向量 randNumbers[] 的参数等同于 vector* randNumbers 将参数更改为

bool numInList(vector<int> &randNumbers, int num)

您应该通过 reference 传递向量(除非您想复制它)。如果您不打算修改矢量,请将其设为 const 参考:

bool numInList(const vector<int>& randNumbers, int num)

更多信息:How to pass objects to functions in C++?(这些是 C++ 的基础知识。正确理解它们。)

此外,您可以使用 vector::size 来获取向量中元素的数量。这样 index 将永远不会超过向量的大小,并且无论向量的大小是多少,您都不会越界访问:

for (int index = 0; index < randNumbers.size(); index++)

在这里,我重写了整个内容以向您展示应如何使用向量。仔细阅读并确保您理解所有内容(包括为什么您的原始方法不是最佳设计选择):

int main()
{
  const int length = 100;
  vector<int> randNumbers;
  vector<int> newList;

  srand(time(0));

  while(randNumbers.size() < length){
    int num = rand() % 90000 +10000;

    if (!numInList(randNumbers, num)){
      randNumbers.push_back(num);
      cout << "The Random Number  " << randNumbers.back() << endl;
    }
  }

  cout << "\n\n\n";
  newList = randNumbers;

  return 0;
}

bool numInList(const vector<int>& randNumbers, int num){
  for (int index = 0; index < randNumbers.size(); index++){
    if (randNumbers[index] == num){
      return true;
    }
  }
  return false;
}