2次元ベクトルの行のサイズを変更するにはどうすればよいですか?ベクトルのサイズを変更するには(行のみ)
vector< vector<int> > matrix;
matrix.resize(num_of_row , vector<int>("I don't know how big the cols "));
2次元ベクトルの行のサイズを変更するにはどうすればよいですか?ベクトルのサイズを変更するには(行のみ)
vector< vector<int> > matrix;
matrix.resize(num_of_row , vector<int>("I don't know how big the cols "));
サイズ変更する前に既存の列数を保存できます。ような何か:
auto num_of_col = matrix[0].size();
matrix.resize(new_num_of_row, std::vector<int>(num_of_col));
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);
matrix.resize(new_no_col,std::vector<int>(old_no_col));
'matrix.resize(num_of_row)です;'あなたのために働いていませんか? –
私はちょうどC + 98;を使用しています。 not C++ 11 – Yongil
2次元ベクトルではなくベクトルベクトルです。それがあなたの問題の根源です。 –