将2D数组传递给c ++中的函数

时间:2014-05-27 14:33:08

标签: c++ function multidimensional-array

我遇到将2D数组传递给c ++函数的问题。该函数应该打印2D数组的值。但是得到错误 在功能void showAttributeUsage(int)中 数组下标的int(int)类型无效 我知道问题在于我将特定数组传递给函数的语法,但我不知道如何解决这个特定问题。
代码:

#include <iostream>

using namespace std;
void showAttributeUsage(int);
int main()
{
    int qN, aN;
    cout << "Enter Number of Queries : ";
    cin >> qN;
    cout << "\nEnter Number of Attributes : ";
    cin >> aN;
    int attVal[qN][aN];
    cout << "\nEnter Attribute Usage Values" << endl;
    for(int n = 0; n < qN; n++) { //for looping in queries
        cout << "\n\n***************** COLUMN " << n + 1 << " *******************\n\n";
        for(int i = 0; i < aN; i++) {     //for looping in Attributes
LOOP1:
            cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
            cin >> attVal[n][i];
            cout << endl;
            if((attVal[n][i] > 1) || (attVal[n][i] < 0)) {
                cout << "\n\nTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Values\n\n";
                goto LOOP1;                  //if wrong input value
            }

        }

    }
    showAttributeUsage(attVal[qN][aN]);
    cout << "\n\nYOUR ATTRIBUTE USAGE MATRIX IS\n\n";

    getch();
    return 0;

}
void showAttributeUsage(int att)
{
    int n = 0, i = 0;
    while(n != '\0') {
        while(i != '\0') {
            cout << att[n][i] << " ";
            i++;

        }
        cout << endl;
        n++;
    }
}

7 个答案:

答案 0 :(得分:1)

我真的建议您使用std::vectorlive example

void showAttributeUsage(const std::vector<std::vector<int>>& att)
{
    for (std::size_t n = 0; n != att.size(); ++n) {
        for (std::size_t i = 0; i != att.size(); ++i) {
            cout << att[n][i] << " ";
        }
        cout << endl;
    }
}

然后这样称呼:

showAttributeUsage(attVal);

答案 1 :(得分:1)

查看您的代码,我认为没有理由不使用std::vector

首先,您的代码使用非标准C ++扩展,即Variable Length Arrays(VLA)。如果您的目标是编写标准C ++代码,那么您编写的内容不是有效的标准C ++。

其次,您最初尝试传递int是错误的,但如果您使用vector,如果您使用{int,则传递vector的尝试看起来几乎相同1}}。

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

typedef std::vector<int> IntArray;
typedef std::vector<IntArray> IntArray2D;

using namespace std;
void showAttributeUsage(const IntArray2D&);
int main()
{
   int qN, aN;
   cout << "Enter Number of Queries : ";
   cin >> qN;
   cout << "\nEnter Number of Attributes : ";
   cin >> aN;
   IntArray2D attVal(qN, IntArray(aN));
   //...  Input left out ...
   showAttributeUsage(attVal);
   return 0;
}

void showAttributeUsage(const IntArray2D& att)
{
   for_each(att.begin(), att.end(), 
            [](const IntArray& ia) {std::copy(ia.begin(), ia.end(), ostream_iterator<int>(cout, " ")); cout << endl;});
}

我省略了代码的输入部分。 vector使用[]就像常规数组一样,因此一旦声明了向量,就不必重写代码。您可以使用 molbdnilo 在其他答案中为您输入的代码来输入数据(不使用goto)。

其次,只是将其抛入混合,showAttributeUsage函数使用copy算法输出信息。 for_each抛出向量的每一行,为元素行调用std::copy。如果您使用的是兼容C ++ 11的编译器,则应编译以上内容。

答案 2 :(得分:0)

你应该声明这样的函数。

void array_function(int m, int n, float a[m][n])
{
      for (int i = 0; i < m; i++)
          for (int j = 0; j < n; j++)
              a[i][j] = 0.0;
}

传递数组的维度。

答案 3 :(得分:0)

这个问题已经回答here。您需要使用指针或模板。其他解决方案也存在。

总之做这样的事情:

template <size_t rows, size_t cols>
void showAttributeUsage(int (&array)[rows][cols])
{
    for (size_t i = 0; i < rows; ++i)
    {
        std::cout << i << ": ";
        for (size_t j = 0; j < cols; ++j)
            std::cout << array[i][j] << '\t';
        std::cout << std::endl;
    }
}

答案 4 :(得分:0)

您正在使用编译器扩展,该扩展允许您声明在运行时确定大小的数组 没有办法将具有此类维度的2D数组传递给函数,因为在编译时必须知道数组作为函数参数的所有维度。

您可以使用固定尺寸并使用读取的值作为传递给函数的限制:

const int max_queries = 100; 
const int max_attributes = 100;

void showAttributeUsage(int array[max_queries][max_attributes], int queries, int attributes);


int main()
{
    int attVal[max_queries][max_attributes];
    int qN = 0;
    int aN = 0;
    cout << "Enter Number of Queries (<= 100) : ";   
    cin >> qN;
    cout << "\nEnter Number of Attributes (<= 100) : ";   
    cin >> aN;

    cout << "\nEnter Attribute Usage Values" << endl;
    for (int n = 0; n < qN; n++)
    {
        cout << "\n\n***************** COLUMN " << n + 1 <<" *******************\n\n";
        for (int i = 0; i < aN; i++)
        {
            bool bad_input = true;
            while (bad_input)
            {
                bad_input = false;  // Assume that input will be correct this time.
                cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
                cin >> attVal[n][i];
                cout << endl;
                if (attVal[n][i] > 1 || attVal[n][i] < 0)
                { 
                    cout << "\n\nTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Values\n\n";
                    bad_input = true;
                }
            }
        }
    }
    cout << "\n\nYOUR ATTRIBUTE USAGE MATRIX IS\n\n";
    showAttributeUsage(attVal, qN, aN);

    getch();
    return 0;   
}

void showAttributeUsage(int att[max_queries][max_attributes], int queries, int attributes)
{
    for (int i = 0; i < queries; i++)
    {
        for (int j = 0; j < attributes; j++)
        {
            cout << att[i][j] << " ";
        }
        cout << endl;
    }
}    

为了比较,使用std::vector的相同程序,几乎相同但没有大小限制:

void showAttributeUsage(vector<vector<int> > att);

int main()
{
    cout << "Enter Number of Queries (<= 100) : ";   
    cin >> qN;
    cout << "\nEnter Number of Attributes (<= 100) : ";   
    cin >> aN;

    vector<vector<int> > attVal(qN, vector<int>(aN));

    cout << "\nEnter Attribute Usage Values"<<endl;
    for (int n = 0; n < qN; n++)
    {
        cout<<"\n\n***************** COLUMN "<<n+1<<" *******************\n\n";
        for (int i = 0; i < aN; i++)
        {
            bool bad = true;
            while (bad)
            {
                bad = false;
                cout << "Use(Q" << n + 1 << " , " << "A" << i + 1 << ") = ";
                cin >> attVal[n][i];
                cout << endl;
                if (attVal[n][i] > 1 || attVal[n][i] < 0)
                { 
                    cout << "\n\nTHE VALUE MUST BE 1 or 0 . Please Re-Enter The Values\n\n";
                    bad = true;
                }
            }
        }
    }
    cout << "\n\nYOUR ATTRIBUTE USAGE MATRIX IS\n\n";
    showAttributeUsage(attVal);

    getch();
    return 0;   
}

void showAttributeUsage(vector<vector<int> > att);
{
    for (int i = 0; i < att.size(); i++)
    {
        for (int j = 0; j < att[i].size(); j++)
        {
            cout << att[i][j] << " ";
        }
        cout << endl;
    }
}    

答案 5 :(得分:0)

特殊逻辑为我工作。终于找到了。 : - )

int** create2dArray(int rows, int cols) {

    int** array = new int*[rows];

    for (int row=0; row<rows; row++) {

        array[row] = new int[cols];

    }

    return array;

}



void delete2dArray(int **ar, int rows, int cols) {

    for (int row=0; row<rows; row++) {

        delete [] ar[row];

    }

    delete [] ar;

}


void loadDefault(int **ar, int rows, int cols) {

    int a = 0;

    for (int row=0; row<rows; row++) {

        for (int col=0; col<cols; col++) {

            ar[row][col] = a++;

        }

    }

}



void print(int **ar, int rows, int cols) {

    for (int row=0; row<rows; row++) {

        for (int col=0; col<cols; col++) {

            cout << " | " << ar[row][col];

        }

        cout << " | " << endl;

    }

}

int main () {

    int rows = 0;

    int cols = 0;

    cout<<"ENTER NUMBER OF ROWS:\t";cin>>rows;
    cout<<"\nENTER NUMBER OF COLUMNS:\t";cin>>cols;
    cout<<"\n\n";

    int** a = create2dArray(rows, cols);

    loadDefault(a, rows, cols);

    print(a, rows, cols);

    delete2dArray(a, rows, cols);
    getch();
    return 0;

}

答案 6 :(得分:0)

如果使用的是c ++,则可以使用适用于任意尺寸的模板

template<typename T>
void func(T& v)
{
    // code here
}

int main()
{
    int arr[][7] = {
        {1,2,3,4,5,6,7},
        {1,2,3,4,5,6,7}
    };
    func(arr);
    char triplestring[][2][5] = {
        {
            "str1",
            "str2"
        },
        {
            "str3",
            "str4"
        }
    };
    func(triplestring);
    return 0;
}