我编写了一个生成唯一随机数的小程序。我首先使用我所知道的数组编写它来加载和打印数字。我正在尝试用 vector 替换数组,所以如果我想复制列表,我可以更轻松地做到这一点。我遇到错误。
error: cannot convert 'std::vector<int>' to "std::vector<int>*' for argument '1' to bool numInList(std::vector<int>*, int)'
当我调用 numInList 函数时出现此错误。
我不熟悉使用 vector ,但我认为您可以像数组一样使用 vector ,它具有内置函数、没有固定大小以及将一个 vector 复制到另一个 vector 的能力等优点。
这是我的代码:
#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 (除非您想复制它)。如果您不打算修改 vector ,请将其设为const 引用:
bool numInList(const vector<int>& randNumbers, int num)
更多信息:How to pass objects to functions in C++? (这些是 C++ 的基础知识。正确使用它们。)
此外,您可以使用 vector::size
获取 vector 中元素的数量。这样 index
将永远不会超过 vector 的大小,并且无论 vector 的大小是多少,您都不会越界访问:
for (int index = 0; index < randNumbers.size(); index++)
在这里,我为您重写了整个内容,以展示应如何使用 vector 。仔细阅读并确保您理解所有内容(包括为什么您的原始方法不是最佳设计选择):
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;
}
关于c++ - 使用 vector 而不是数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157549/