简体   繁体   English

使用向量而不是数组

[英]Using Vectors instead of Arrays

I have wrote a small program that generates unique random numbers. 我写了一个小程序,可以生成唯一的随机数。 I wrote it first using what I know, an array, to load and print the numbers. 我首先使用我所知道的数组(即数组)来加载和打印数字。 I am trying to replace the array with a vector so if I want make copies of the list I can do that easier. 我正在尝试用向量替换数组,因此,如果要复制列表,可以更轻松地完成。 I am getting a error. 我收到一个错误。

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

this error occurs when I call the numInList function. 当我调用numInList函数时,会发生此错误。

I am new at using vectors but I thought you could use vectors like arrays with the benefits of built in functions, no fixed size, and the ability to copy one vector into another vector. 我是使用向量的新手,但我认为您可以使用向量(如数组),因为它具有内置函数的优点,没有固定大小,并且能够将一个向量复制到另一个向量中。

here is my code: 这是我的代码:

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

I tried de-referencing hoping that would solve the problem 我尝试取消引用,希望可以解决问题

  if (!numInList(&randNumbers, num))

then I get an error on the IF statement in the function numInList 然后我在函数numInList中的IF语句上收到错误

 error: ISO C++ forbids comparisons between pointers and integer [f-permissive]  

Any help would be greatly appreciated. 任何帮助将不胜感激。


I have changed a few things, now I don't get any compilation errors but the program crashes when executed ... any suggestions??? 我更改了几件事,现在没有任何编译错误,但是执行时程序崩溃了……有什么建议???

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

You should pass vectors around by reference (unless you want to copy it). 您应该通过引用传递矢量(除非您要复制它)。 If you're not going to modify the vector, make it a const reference: 如果您不打算修改向量,请使其成为const引用:

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

More info: How to pass objects to functions in C++? 更多信息: 如何将对象传递给C ++中的函数? (These are the very basics of C++. Get them right.) (这些是C ++的基本知识。正确使用它们。)

Also, you can use vector::size to get the number of elements in a vector. 另外,您可以使用vector::size获取向量中元素的数量。 This way index will never exceed the size of the vector and you won't have an out-of-bounds access, no matter what the size of the vector is: 这样, index将永远不会超出向量的大小,并且无论向量的大小是多少,您都将无法进行越界访问:

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

Here, I rewrote the whole thing for you to show how a vector should be used. 在这里,我为您重写了整个内容,以显示应如何使用向量。 Read it through carefully and make sure you understand everything (including why your original approach was not the best possible design choice): 仔细阅读并确保您理解所有内容(包括为什么原始方法不是最佳的设计选择):

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM