使用矢量而不是数组

Using Vectors instead of Arrays

本文关键字:数组      更新时间:2023-10-16

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

  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 << "nnn";
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 << "nnn";
     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;
   }

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

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

更多信息:如何在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 << "nnn";
  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;
}

numInList vector randNumbers[]的参数等效于vector*randNumbers将参数更改为

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