[英]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;
}
您应该通过引用传递矢量(除非您要复制它)。 如果您不打算修改向量,请使其成为const引用:
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 << "\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;
}
numInList vector randNumbers []的参数等效于vector * randNumbers将参数更改为
bool numInList(vector<int> &randNumbers, int num)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.