如何调整矢量大小(仅限行)

时间:2017-08-24 04:17:03

标签: c++ c++11

如何仅调整2d-vector中的行?

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

3 个答案:

答案 0 :(得分:0)

您可以在调整大小之前保存现有列数。类似的东西:

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

答案 1 :(得分:0)

如果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);

答案 2 :(得分:0)

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { guard let flowLayout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout else { return } if UIInterfaceOrientationIsLandscape(UIApplication.shared.statusBarOrientation) { //here you can do the logic for the cell size if phone is in landscape } else { //logic if not landscape } let cellSize = CGSize(width: (size.width - CGFloat(row ))/CGFloat(row) , height: 90) flowLayout.itemSize = cellSize flowLayout.invalidateLayout() }