2

私はプログラミングに慣れていないので、2次元配列(または私の場合は行列)を入力して後で印刷できるコードを書きたいと思います。

#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]型が無効です」などのエラーが発生します。私のコードを確認して、私の欠陥を指摘していただけますか?

ありがとう

4

5 に答える 5

6

あなたは一次元だけであると宣言yしました。2次元であるint*と宣言する必要がありますyint**

コードがコンパイルされない理由は、単一のメモリブロック(つまり、整数の配列、つまり、一連のs)int* yを指しているためです。はこの配列内のsの1つであるため、をに割り当てようとしているため、コンパイルに失敗します。inty[w]inty[w] = new int[n]int*int

の配列を指すことができる手段に変更yします。それぞれがの配列を指すことができるため、2次元配列になります。int**yint*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;ます。これは、要素を追加したいときに便利に拡張し、破棄されたときにメモリを解放することで、メモリを処理するためです。

于 2012-07-30T16:45:44.470 に答える
1

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;
于 2012-07-30T16:46:49.517 に答える
0

int * y=new int [n];動的に割り当てられたint値の配列へのポインタです。

y[w] = new int [n];配列の要素にポインタを割り当てようとします。

学習演習として、生の配列をいじることは良い考えです。それらの使用方法を理解すれば、それらの使用を(非常に)停止し、のような自動ストレージタイプを使用するのに十分な知識が得られますstd::vector

于 2012-07-30T16:45:33.487 に答える
0

y1)次の配列へのポインタとして宣言したいint

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

2)printArray配列ではなく、行列を扱っています。マトリックスのメンバーにアクセスするには、次のforような2つのネストされたループを使用します。

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

3)メソッドへintのポインタではなく、の配列へのポインタを渡す必要があります。intprintArray

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

ここで上記の修正を加えた動作中のコードを見つけてください:http://ideone.com/2h9dR

于 2012-07-30T16:50:58.657 に答える
0

まず、他の回答を読んでから、C++の優れた本のポインタの章をもう一度読んでください。さて、あなたが極端な速度を必要としない限り、のvector使用。C ++ 11(新しいC ++標準)では、それは本当に素晴らしくて読みやすいので、最初にそれを投稿します:vectordouble

#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))を作成することを意味することに注意してください。を使用して、値を取得および設定することもできます。代わりにy.at(1).at(2)を使用すると、適切なチェックが行われるため、範囲外で読み取りまたは書き込みを行った場合に例外が発生します。nny[1][2]

于 2012-07-30T17:25:39.840 に答える