【问题标题】:Using Vectors instead of Arrays使用向量而不是数组
【发布时间】:2025-12-04 13:40:01
【问题描述】:

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

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

【问题讨论】:

  • vector &lt;int&gt; randNumbers[]randNumbers 声明为 of 个向量的数组。或者实际上,作为指向向量的指针,因为它是一个函数参数。 newlist[] = randNumbers[] 也应该只是 newlist = randNumbers
  • Zenith,你将如何正确声明向量?我尝试删除括号 [ ],但我得到了同样的错误。
  • @bryan,在函数的声明定义中进行。
  • 如果您不想重复,为什么不使用std::set&lt;int&gt; 而不是向量?如果你这样做了,numInList 就不需要了。
  • 感谢 Zenith,我需要休息一下,我认为您的解决方案很有意义,我会尝试一下。

标签: c++ arrays vector


【解决方案1】:

你应该通过 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;
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 加里,我试过了。我收到一条错误消息,提示 C++ 禁止指针和整数之间的比较 [f-permissive] ...
    • 删除 newlist[] = randNumbers[];
    • 我已经删除了所有的 [] 和 (),现在程序崩溃了……我使用数组让它正确运行。我已经发布了我编辑的代码。
    • @bryan 向量通过调用vector::size 函数知道它的大小。无需使用变量来跟踪大小。我说的是您的 for 循环,它使用了 length 变量。为什么要冒险使用可能具有或不具有正确值的变量?只需调用size() 成员函数即可获得真实长度。
    • @bryan randNumbers[countCheck] 将其替换为 vector::back() 函数以获得向量中的最后一项。同样,尽可能使用向量的成员函数,并停止使用单独的变量来索引向量。同样的事情:while(countCheck &lt; length) -- countCheck 只不过是向量的size(),所以去掉 countCheck。