无法将vector 转换为int *以用于bool testPIN

时间:2021-03-08 02:57:33

For a class assignment I had to rewrite this code using vectors instead of arrays. I figured I would use the existing arrays and assign them as a vector. But I get:

对于类赋值,我必须使用向量而不是数组重写此代码。我想我会使用现有的数组并将它们指定为矢量。但我得到:

error: cannot convert 'std::vector' to 'int*' for argument '1' to 'bool testPIN(int*, int*, int)'

错误:无法将参数'1'的'std :: vector'转换为'int *'为'bool testPIN(int *,int *,int)'

How can I get around this error?

我怎样才能解决这个错误?

#include <iostream>
#include<vector>

using namespace std;

// Function Prototype

bool testPIN(int [], int [], int);

int main ()
{
    const int NUM_DIGITS = 7; // Number of digits in a PIN

    int cpin1[NUM_DIGITS] = {2, 4, 1, 8, 7, 9, 0}; // Base set of values.
    int cpin2[NUM_DIGITS] = {2, 4, 6, 8, 7, 9, 0}; 
    int cpin3[NUM_DIGITS] = {1, 2, 3, 4, 5, 6, 7}; 

    vector<int> pin1(cpin1, cpin1+7) ;
    vector<int> pin2(cpin2, cpin2+7) ;
    vector<int> pin3(cpin3, cpin3+7) ;

    if (testPIN(pin1, pin2, NUM_DIGITS))
        cout << "ERROR: pin1 and pin2 report to be the same.\n";

    else
        cout << "SUCCESS: pin1 and pin2 are different.\n";

    if (testPIN(pin1, pin3, NUM_DIGITS))
        cout << "ERROR: pin1 and pin3 report to be the same.\n";

    else
        cout << "SUCCESS: pin1 and pin3 are different.\n";

    if (testPIN(pin1, pin1, NUM_DIGITS))
        cout << "SUCCESS: pin1 and pin1 report to be the same.\n";

    else
        cout << "ERROR: pin1 and pin1 report to be different.\n";

    return 0;

}



bool testPIN(int custPIN[], int databasePIN[], int size)
{................}

2 个解决方案

#1


5  

In these situations, it helps to read a good reference. You would need to get the vector's underlying data array:

在这些情况下,有助于阅读一个很好的参考。您需要获取向量的基础数据数组:

testPIN(pin1.data(), pin2.data(), NUM_DIGITS))

If your implementation does not support C++11, you can do the following:

如果您的实现不支持C ++ 11,则可以执行以下操作:

testPIN(&pin1[0], &pin2[0], NUM_DIGITS))

But if you have been asked to re-implement some code to use vectors, you may want to re-implement the testPIN function instead:

但是如果你被要求重新实现一些代码来使用向量,你可能需要重新实现testPIN函数:

bool testPIN(const std::vector<int>& custPIN1,
             const std::vector<int>& custPIN2);

and then just pass the vectors:

然后传递矢量:

testPIN(pin1, pin2);

#2


1  

testPIN(&pin1[0], &pin2[0], NUM_DIGITS)

because the vector's internal memory structure is same with array.

因为向量的内部存储器结构与数组相同。

Please check this.

请检查一下。

How to convert vector to array in C++

如何在C ++中将向量转换为数组

#1


5  

In these situations, it helps to read a good reference. You would need to get the vector's underlying data array:

在这些情况下,有助于阅读一个很好的参考。您需要获取向量的基础数据数组:

testPIN(pin1.data(), pin2.data(), NUM_DIGITS))

If your implementation does not support C++11, you can do the following:

如果您的实现不支持C ++ 11,则可以执行以下操作:

testPIN(&pin1[0], &pin2[0], NUM_DIGITS))

But if you have been asked to re-implement some code to use vectors, you may want to re-implement the testPIN function instead:

但是如果你被要求重新实现一些代码来使用向量,你可能需要重新实现testPIN函数:

bool testPIN(const std::vector<int>& custPIN1,
             const std::vector<int>& custPIN2);

and then just pass the vectors:

然后传递矢量:

testPIN(pin1, pin2);

#2


1  

testPIN(&pin1[0], &pin2[0], NUM_DIGITS)

because the vector's internal memory structure is same with array.

因为向量的内部存储器结构与数组相同。

Please check this.

请检查一下。

How to convert vector to array in C++

如何在C ++中将向量转换为数组