'int'问题

Problems with 'int'

本文关键字:问题 int      更新时间:2023-10-16

我是编程新手,所以我想写一段代码,让我输入一个二维数组(在我的情况下是一个矩阵),然后打印出来。

#include <iostream>
using namespace std;
void printArray( const int *array, int count ) 
    { 
       for ( int i = 0; i < count; i++ ) 
          cout << array[ i ] << " "; 
       cout << endl; 
    }
int main () {
int n;
cout<<"Please enter the length of your matrix : "<<endl;
cin>>n;
int * y=new int [n];
for (int w = 0; w <= n-1; w++ ) {
    y[w] = new int [n];
    cout<<"Insert the elements ";
            for (int z = 0; z <= n-1; z++)
            {
                cin >>y [w][z];
            }   
}
printArray(y, n);
}

然而,我会遇到诸如"从'int*'到'int'的无效转换"answers"数组下标的无效类型int[int]"之类的错误。你能检查一下我的代码并指出我的缺陷吗?

感谢

您将y声明为仅为一维的int*。您需要将y声明为int**才能使其为2维。

代码不编译的原因是int* y指向一个单个内存块(即整数数组,换句话说,一堆int s)。y[w]是该数组中的int之一,因此y[w] = new int[n]无法编译,因为您正试图将int*分配给int

y更改为int**意味着y可以指向一个int*s的数组。由于每个int*都可以指向int的数组,因此您将拥有一个二维数组

int**的10x10矩阵的示例代码:

int** iShouldUseStdVector = new int*[10]; // allocate 10 int* <--
for (int i = 0; i < 10; i++)
{
    iShouldUseStdVector[i] = new int[10]; // allocate 10 int <--
    for (int k = 0; k < 10; k++)
    {
        iShouldUseStdVector[i][k] = k;
    }
}

带有std::vector的10x10矩阵的示例代码:

std::vector<std::vector<int>> thisIsEasy;
for (int i = 0; i < 10; i++)
{
    thisIsEasy.push_back(std::vector<int>());
    for (int k = 0; k < 10; k++)
    {
        thisIsEasy[i].push_back(k);
    }
}

我建议使用std::vector<std::vector<int>> y;,因为它可以方便地在您想要添加更多元素时进行增长,并在内存被破坏时释放内存,从而为您处理内存。

int*y=新的int[n];

这是一个长度为CCD_ 21的数组。您需要的是:

int **y = new int*[n];
for (int i=0; i<n; i++)
    y[i] = new int[n];
....
//delete[] y[i] in a loop
delete[] y;

既然你使用的是C++,为什么不呢:

#include <vector>
...
std::vector<std::vector<int> > matrix;

int * y=new int [n];是指向动态分配的int值数组的指针;

CCD_ 24尝试将指针分配到数组的元素中。

作为一种学习练习,摆弄原始数组是个好主意。一旦你知道如何使用它们,你就会知道停止使用它们(非常多),转而使用像std::vector这样的自动存储类型。

1)您想要将y声明为指向int:数组的指针

int ** y = new int* [n];

2) 在printArray中,您处理的是矩阵,而不是数组。要访问矩阵的成员,请使用两个嵌套的for循环,如下所示:

for (int i = 0; i < nLines; i++) {
    for (int j = 0; i < nColumns; j++) {
        std::cout << matrix[i][j] << std::endl;
    }
}

3) 您需要将指向int数组的指针传递给printArray方法,而不是指向int的指针。

void printArray( const int **array, int count ) 

在此处查找工作代码,并进行上述修复:http://ideone.com/2h9dR

首先,阅读其他答案,然后可能重读一本好的C++书中的指针章节。现在,除非您需要极高的速度,否则请使用vectorvectordouble。有了C++11(较新的C++标准),它真的很好,可读性很强,所以我先发布它:

#include <iostream>
#include <vector>
void printArray( std::vector< std::vector<double> > & v ) {
   for ( const auto & row : v ){
      for ( const auto & value : row ){
        std::cout << value << " ";
      }
      std::cout << std::endl;
   }
}
int main () {
   int n;
   std::cout<<"Please enter the length of your matrix : "<<std::endl;
   std::cin>>n;
   std::vector<std::vector<double>> y(n,std::vector<double>(n,0));
   for ( auto & row : y ){
      std::cout<<"Insert the elements of row :";
      for ( auto & value : row ){
        std::cin >> value;
      }   
   }
   printArray(y);   
}

对于较旧的C++,它是这样的:

void printArray( std::vector< std::vector<double> > & v ) {
   for ( std::vector<std::vector<double> >::const_iterator it = v.begin(); it != v.end();it++){
      for ( std::vector<double>::const_iterator it2 = it->begin(); it2!= it->end();it2++) {
     std::cout << (*it2) << " ";
      }
      std::cout << std::endl;
   }
}
int main () {
   int n;
   std::cout<<"Please enter the length of your matrix : "<<std::endl;
   std::cin>>n;
   std::vector<std::vector<double> > y(n,std::vector<double>(n,0));
   for ( std::vector<std::vector<double> >::iterator it = y.begin(); it!= y.end();it++){
      std::cout<<"Insert the elements of row :";
      for ( std::vector<double>::iterator it2 = it->begin(); it2!= it->end();it2++) {
     std::cin >> (*it2);
      }   
   }
   printArray(y);   
}

注意,y(n,std::vector<double>(n,0))意味着使n向量,每个向量具有n零。您也可以使用y[1][2]来获取和设置值。如果你使用y.at(1).at(2),你会得到正确的检查,这样当你读写越界时就会得到一个异常。