【问题标题】:Problems with 'int'“int”的问题
【发布时间】:2012-07-30 16:42:01
【问题描述】:

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

#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' 的无效转换”和“数组下标的无效类型 int[int]”之类的错误。你能检查我的代码并指出我的缺陷吗?

谢谢

【问题讨论】:

  • @RyanGray 如果数组不是固定大小(它不是),那么它必须被更新。当然,应该使用 std::vector 这样的结构来代替。
  • @RyanGray:使用new[]是一种合法的创建数组的方式,和new不一样。
  • 你需要使用双指针,你的数组 y 需要声明为 int ** y = new int* [n]

标签: c++ arrays multidimensional-array user-input


【解决方案1】:

您将y 声明为int*,它只是一维的。您需要将 y 声明为 int** 才能使其为二维。

您的代码无法编译的原因是因为int* y 指向一个单个 内存块(这是一个整数数组,换句话说,是一堆ints。) . y[w] 是该数组中的 ints 之一,因此 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&lt;std::vector&lt;int&gt;&gt; y;,因为它会在您想要添加更多元素时方便地增长并在内存被破坏时释放内存,从而为您处理内存。

【讨论】:

    【解决方案2】:

    int * y=new int [n];

    这是一个长度为n 的数组。你需要的是:

    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;
    

    【讨论】:

      【解决方案3】:

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

      y[w] = new int [n]; 尝试将指针分配给数组的元素。

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

      【讨论】:

        【解决方案4】:

        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 数组的指针,而不是指向int 的指针传递给printArray 方法。

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

        在此处找到带有上述修复的工作代码:http://ideone.com/2h9dR

        【讨论】:

        • 感谢您的帮助!感谢链接。
        【解决方案5】:

        首先,阅读其他答案,然后再阅读一本好的 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&lt;double&gt;(n,0)) 表示创建n 向量,每个向量都有n 零。您还可以使用y[1][2] 来获取和设置值。如果您使用 y.at(1).at(2) 代替,您会得到适当的检查,以便在读取或写入越界时得到异常。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-13
          • 2011-07-12
          • 1970-01-01
          • 2017-06-01
          • 2011-08-24
          • 2013-09-11
          • 1970-01-01
          • 2016-10-03
          相关资源
          最近更新 更多