I tried this but it is not working ! can any one help me please this is very important :(
我尝试了这个,但它没有用!任何人都可以帮助我,这是非常重要的:(
#include <iostream>
using namespace std;
int a[100][100];
void read(int a[][100],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
int main ()
{
int n;
cin>>n;
int a[n][n];
read(a,n);
}
2 个解决方案
#1
2
The unclear syntax to pass array by reference is:
通过引用传递数组的不明确语法是:
void read(int (&a)[100][100], int n)
resulting in
#include <iostream>
void read(int (&a)[100][100], int n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
std::cin >> a[i][j];
}
int main ()
{
int n;
std::cin >> n;
int a[100][100];
read(a, n);
}
but you might prefer std::vector
:
但你可能更喜欢std :: vector:
#include <iostream>
#include <vector>
void read(std::vector<std::vector<int>> &mat)
{
for (auto& v : mat) {
for (auto& e : v) {
std::cin >> e;
}
}
}
int main ()
{
int n;
std::cin >> n;
std::vector<std::vector<int>> mat(n, std::vector<int>(n));
read(mat);
}
#2
0
Since this is tagged C++. I'd like to suggest usage of std::vector. It's dynamic container which is very useful. You can resize it, clear it fill it with ease. Once you understand it's basic usage, they would come really handy in your future C++ development. I modified your code slightly:
因为这是标记的C ++。我想建议使用std :: vector。它是动态容器,非常有用。您可以调整它的大小,清除它可以轻松填充它。一旦你理解了它的基本用法,它们将在你未来的C ++开发中变得非常方便。我稍微修改了你的代码:
#include <iostream>
#include <vector>
using namespace std;
void read(vector<vector<int> >& arr,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
}
int main ()
{
int N;
cin>>N;
vector<vector<int> > arr(N, vector<int>(N));
read(arr, N);
}
They have many advantages over the primitive arrays like they can be initialized easily, suppose you want to initialize all to zero :
它们比原始数组有许多优点,比如它们可以很容易地初始化,假设你想要将所有数据初始化为零:
vector<vector<int> > arr(N, vector<int>(N, 0));
You don't have to worry about adding the array size whenever passing in functions. vector can easily handle this:
传入函数时,您不必担心添加数组大小。矢量可以轻松处理这个:
for(i = 0; i < arr.size(); i++) {
for(j = 0; j < arr[i].size(); j++) {
// do stuff
}
}
Moreover with the added methods of the Standard template library like fill
, swap
. Many operations can be handled easily.
此外,添加了标准模板库的方法,如fill,swap。许多操作都可以轻松处理。
#1
2
The unclear syntax to pass array by reference is:
通过引用传递数组的不明确语法是:
void read(int (&a)[100][100], int n)
resulting in
#include <iostream>
void read(int (&a)[100][100], int n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
std::cin >> a[i][j];
}
int main ()
{
int n;
std::cin >> n;
int a[100][100];
read(a, n);
}
but you might prefer std::vector
:
但你可能更喜欢std :: vector:
#include <iostream>
#include <vector>
void read(std::vector<std::vector<int>> &mat)
{
for (auto& v : mat) {
for (auto& e : v) {
std::cin >> e;
}
}
}
int main ()
{
int n;
std::cin >> n;
std::vector<std::vector<int>> mat(n, std::vector<int>(n));
read(mat);
}
#2
0
Since this is tagged C++. I'd like to suggest usage of std::vector. It's dynamic container which is very useful. You can resize it, clear it fill it with ease. Once you understand it's basic usage, they would come really handy in your future C++ development. I modified your code slightly:
因为这是标记的C ++。我想建议使用std :: vector。它是动态容器,非常有用。您可以调整它的大小,清除它可以轻松填充它。一旦你理解了它的基本用法,它们将在你未来的C ++开发中变得非常方便。我稍微修改了你的代码:
#include <iostream>
#include <vector>
using namespace std;
void read(vector<vector<int> >& arr,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
}
int main ()
{
int N;
cin>>N;
vector<vector<int> > arr(N, vector<int>(N));
read(arr, N);
}
They have many advantages over the primitive arrays like they can be initialized easily, suppose you want to initialize all to zero :
它们比原始数组有许多优点,比如它们可以很容易地初始化,假设你想要将所有数据初始化为零:
vector<vector<int> > arr(N, vector<int>(N, 0));
You don't have to worry about adding the array size whenever passing in functions. vector can easily handle this:
传入函数时,您不必担心添加数组大小。矢量可以轻松处理这个:
for(i = 0; i < arr.size(); i++) {
for(j = 0; j < arr[i].size(); j++) {
// do stuff
}
}
Moreover with the added methods of the Standard template library like fill
, swap
. Many operations can be handled easily.
此外,添加了标准模板库的方法,如fill,swap。许多操作都可以轻松处理。