2017-04-24 10 views
1

2次元ベクトルを与えて、要素をランダムに取り出してから削除し、ベクトルが空になるまでこのプロセスを繰り返します。セグメンテーションフォールト:2次元ベクトルから要素をランダムに削除する

しかし、私のコードは毎回ループの異なるポイントで、実行するとSegmentation fault: 11エラーを返します。これは、コードがもはや存在しないインデックスから要素を取得しようとしていることと、インデックスを解析する方法や要素を間違って削除する方法について考えてきたことを示しています。

この問題を解決する方法の提案はありますか?

#include <vector> 
#include <iostream> 

int main(void) { 

    int X_LENGTH = 4; 
    int Y_LENGTH = 4; 
    std::vector<std::vector<long> > arrayCellDistance(X_LENGTH, std::vector<long>(Y_LENGTH, 0)); 

    // Assign values to array, print them out in order 
    for (int i = 0; i < X_LENGTH; i++) { 
     for (int j = 0; j < Y_LENGTH; j++) { 
      arrayCellDistance[i][j] = (i+j)/2 + i*j; 
      std::cout << "arrayCellDistance[" << i << "][" << j << "] = " << arrayCellDistance[i][j] << std::endl; 
     } 
    } 

    std::cout << "===============================================" << std::endl; 

    int x, y; 
    srand(time(NULL)); 

    while (!arrayCellDistance.empty()) { 

     y = (rand() % (int)(arrayCellDistance.size())); // Rand from 0 to number of rows 
     x = (rand() % (int)(arrayCellDistance[y].size())); // Rand from 0 to number of columns in row 

     // 'Retrieve' value from array and then delete this value 
     std::cout << "arrayCellDistance[" << x << "][" << y << "] = " << arrayCellDistance[x][y] << std::endl; 

     arrayCellDistance[y].erase(arrayCellDistance[x].begin() + 1); // Remove element 

    } 

    return 0; 
} 

除去した後のマトリックスをプリントアウトするとき、私はこの出力を得る:プログラムは2行目2を削除しようとしたときに

arrayCellDistance[0][1] = 0 
0 1 1 0 
2 3 5 
1 3 6 8 
1 5 8 12 
arrayCellDistance[2][2] = 6 
0 1 1 0 
2 3 5 
1 6 8 
1 5 8 12 
arrayCellDistance[1][1] = 3 
0 1 1 0 
2 5 
1 6 8 
1 5 8 12 
arrayCellDistance[2][2] = 8 
0 1 1 0 
2 5 
1 8 
1 5 8 12 
arrayCellDistance[1][0] = 2 
Segmentation fault: 11 

あなたが見ることができるように、セグメンテーションフォールトがあります - だから、まだ "行"ベクトルが存在するので、まだそれはどの行にもアクセスできないはずですか?

+2

'arrayCellDistance.size()+ 1 - 1'?単純に 'arrayCellDistance.size()'ではないのですか? – Borgleader

+0

条件文 'while(!arrayCellDistance.empty())'は、最初のレベルのベクトルが空ではなく、2番目のレベルのベクトルの1つで、2番目のレベルから要素を削除しようとしますベクター? –

+0

@Borgleader良いキャッチ - ランダム関数は、 '+ 1'が含まれているときに' [0、arrayCellDistance.size()] 'の範囲の整数を生成しますが、範囲外のインデックスにアクセスしようとするので' -1'範囲を '[0、arrayCellDistance.size() - 1]に変更するために追加されました。 –

答えて

1

私は今便利なコンパイラを持っていないが、私はあなたのような何かを探していると思う:私たちは、空の第二レベルのベクトルを処理してから消去しようとしていないことを確認したい

while (!arrayCellDistance.empty()) 
{ 
    y = (rand() % (int)(arrayCellDistance.size())); // Rand from 0 to number of rows 

    if(arrayCellDistance[y].empty()) 
    { 
     // Error - Contained empty second-level vector initially. 
     break; 
    } 

    x = (rand() % (int)(arrayCellDistance[y].size())); // Rand from 0 to number of columns in row 

    // Get value from array and display 
    std::cout << "arrayCellDistance[" << x << "][" << y << "] = " << arrayCellDistance[x][y] << std::endl; 

    // Remove element of second-level vector 
    arrayCellDistance[y].erase(arrayCellDistance[y].begin() + x); 

    // Remove empty first-level vector 
    if(array[y].empty()) 
    { 
     arrayCellDistance.erase(arrayCellDistance.begin() + y); 
    } 
} 

彼らは空になった後に彼ら。したがって、このコードは、空になった後に空のベクトルを削除します。

+0

あなたは美しい人間です。ありがとうございました! –

+0

問題はありません:)私は助けてうれしい –

関連する問題