2017-08-24 6 views
0

2次元ベクトルの行のサイズを変更するにはどうすればよいですか?ベクトルのサイズを変更するには(行のみ)

vector< vector<int> > matrix; 
matrix.resize(num_of_row , vector<int>("I don't know how big the cols ")); 
+1

'matrix.resize(num_of_row)です;'あなたのために働いていませんか? –

+0

私はちょうどC + 98;を使用しています。 not C++ 11 – Yongil

+0

2次元ベクトルではなくベクトルベクトルです。それがあなたの問題の根源です。 –

答えて

0

サイズ変更する前に既存の列数を保存できます。ような何か:

auto num_of_col = matrix[0].size(); 
matrix.resize(new_num_of_row, std::vector<int>(num_of_col)); 
0

matrixが空でない場合、あなたは行列の行の1つから、列数を取得することができます。

matrixが空の場合は、関数の入力として列数を指定する必要があります。

if (matrix.size() > 0) 
{ 
    size_t num_of_col = matrix[0].size(); 
    matrix.resize(num_of_row, std::vector<int>(num_of_col)); 
} 
else 
{ 
    matrix.resize(new_num_of_row, std::vector<int>(new_num_of_col)); 
} 

matrixが空であると関数はnew_num_of_colを持っていない場合は、あなたができる最善のは、すべての行が空にされたマトリックスを作成することです。

matrix.resize(new_num_of_row); 
0

matrix.resize(new_no_col,std::vector<int>(old_no_col));

関連する問題