简体   繁体   中英

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.

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

 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:

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

More info: How to pass objects to functions in C++? (These are the very basics of C++. Get them right.)

Also, you can use vector::size to get the number of elements in a vector. 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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