简体   繁体   English

填充数组的C ++函数

[英]c++ function that fills array

What I'm trying to do: 我正在尝试做的是:

User inputs two numbers. 用户输入两个数字。 Array is declared using those numbers as dimensions. 使用这些数字作为维声明数组。 Function outside main() is filling the array. main()外部的函数正在填充数组。 Array is accessed in main() for further thingies. 可以在main()访问数组以进行进一步处理。

What I have problem with: 我有什么问题:

Function + array combination doesn't seem to work as I think. 函数+数组组合似乎不起作用,我认为。

What I did: 我做了什么:

void tablica1(int h, int w)
{
    int m,n;

    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
}

What happens: 怎么了:

array arr is inaccessible in tablica1() because it has not been declared in that function. tablica1()无法访问数组arr,因为尚未在该函数中声明它。

Of course, when I declare the array in tablica1() it becomes inaccessible in main() . 当然,当我在tablica1()声明数组时,它在main()变得不可访问。

Possible solutions: 可能的解决方案:

  • Passing arr to tablica1() as a reference - no idea how to do that 将arr作为参考传递给tablica1() -不知道该怎么做
  • Declaring arr in tablica1() and somehow passing it to main() - no idea how to do that tablica1()声明arr并将其以某种方式传递给main() -不知道该怎么做

Other possible solutions? 其他可能的解决方案?

You can declare the array outside of both, at compilation unit level: 您可以在编译单元级别在两个数组之外声明数组:

int arr[10][10];

void func() {
    for (int i=0; i<10; i++) {
        for (int j=0; j<10; j++) {
            arr[i][j] = i + j;
        }
    }
}

int main(int argc, const char *argv[]) {
    func();
    std::cout << arr[3][4] << "\n"; // Output will be 7
    return 0;
}

If you want handle a dynamically-sized matrix the most common pattern is to use an std::vector of std::vector s (it's a little more general and therefore a little less efficient than a 2d matrix because each row can have a different length, but in most cases the cost difference is not a big issue) 如果要处理动态大小的矩阵,则最常见的模式是使用std::vectorstd::vector (它比2d矩阵更通用,因此效率更低一点,因为每一行可以具有不同的长度,但在大多数情况下,成本差异不是大问题)

#include <vector>

std::vector< std::vector< int > > arr;

void func() {
    int height = arr.size();
    int width = arr[0].size();
    for (int i=0; i<height; i++) {
        for (int j=0; j<width; j++) {
            arr[i][j] = i + j;
        }
    }
}


int main() {
    int height = 13;
    int width = 7;

    arr = std::vector< std::vector<int> >(height, std::vector<int>(width));

    func();

    ...
}

the two solutions you mentioned 您提到的两种解决方案

1、Passing arr to tablica1() as a reference 1,将arr传递给tablica1()作为参考

void tablica1(int h, int w,int **arr)
{
    int m,n;

    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
}

void main()
{
  const int h=100,w=100;
  int arr[h][w];
  tablica1(h,w,arr);
}

2、Declaring arr in tablica1() and somehow passing it to main() 2,在tablica1()中声明arr并将其以某种方式传递给main()

int **tablica1(int h, int w)
{
    int m,n;
    int **arr=new int*[h];
    for(int i=0;i<h;i++)
    {
       arr[i]=new int[w];
    }
    //it is best to initialize arr by setting each element 0
    for(m=0; m<h; m++)
        for(n=0; n<w; n++) 
        {
            arr[h][w]=1;
        }
   return arr;

}

void main()
{
  const int h=100,w=100;
  int **arr=tablica1(h,w);
  //do somting

  //delete arr
  for(int i=0;i<h;i++)
  {
    delete []arr[i];
  }
  delete []arr;
}

If you want to declare a dynamic multidimensional array you can do that with the template give below. 如果要声明动态多维数组,则可以使用下面的模板给出。

     #include<iostream.
     #include <vector>
     using namespace std;
     typedef vector<int> vi;

     vector<vi> arr; // arr is a dynamic two dimensional array.
     vi.assign(10,vi());//10 rows of type vi .

If you want to enter values in arr you can do that by 如果要在arr中输入值,可以通过

    vi[0].push_back(a);
    vi[0].push_back(b);  // a,b,c are some example values..
    vi[1].push_back(c);

You can understand using this code 您可以理解使用此代码

     #include <iostream>
      #include <cstdio>
     #include <vector>
     using namespace std;

     typedef vector <int> vi;

     vector<vi> arr;// dynamic multidimensional array

     int main(){
     cout<<"enter array size\n";
     int h,w,temp;;
     cin>>h>>w;

     arr.assign(h,vi());
     int i, j;
     for(i=0; i < h ;i++)
      for(j=0; j < w; j++)
       {
       cin>>temp;
       arr[i].push_back(temp);
       }

       // for printing
       for(i=0; i < h ;i++){
       for(j=0; j < w; j++)
        {
        cout<<arr[i][j]<<" ";

        }
     cout<<endl;
   }
    return 0;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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