如何調整2d矢量中的行大小?如何調整矢量大小(只有行)
vector< vector<int> > matrix;
matrix.resize(num_of_row , vector<int>("I don't know how big the cols "));
如何調整2d矢量中的行大小?如何調整矢量大小(只有行)
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
不爲空,則可以從矩陣的行中的一個獲得列的數目。
如果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 ;;不是C++ 11 – Yongil
您沒有2d矢量,而是矢量矢量。這是你問題的根源。 – 2017-08-24 07:15:28