如何调整矢量大小(仅行)
How do I resize vector (only rows)
如何在 2D 向量中仅调整行的大小?
vector< vector<int> > matrix;
matrix.resize( num_of_row , vector<int>("I don't know how big the cols ") );
如果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);
您可以在调整大小之前保存现有列计数。像这样:
auto num_of_col = matrix[0].size();
matrix.resize(new_num_of_row, std::vector<int>(num_of_col));
matrix.resize(new_no_col,std::vector<int>(old_no_col));
相关文章:
- 循环仅对第一行正常工作.其他行不受 for 的影响
- 如何调整矢量大小(仅行)
- if 语句仅在前面有调试 cout 行(C 中的多线程)时才通过
- C++ 仅使用一个命令逐行从终端读取文件流
- 仅将某些数据行从 .arff 文件读取到结构C++中
- 当列中的属性与eigen,c 中的给定条件匹配时,仅选择这些行froom a矩阵
- 我想添加两个字符串类型的矩阵.用户将不会输入行数或列的数量.用户将仅输入数组
- C++/CLR:仅在编译 x86 时出现命令行错误
- 2命令行参数中的数字数字仅被视为一个数字
- 带有新行的字符串仅保存在一行中
- 如何仅当字符串流包含任何换行符时才从字符串流中读取行
- 从文件,特定行中仅读取数字
- 仅在向量末尾打印新行 - "if"语句在"for"循环之外
- 当我仅使用链接器选项 /LTCG时,为什么Visual Studio在Linker命令行中显示选项 /PGD
- 仅在从命令行构建Qt项目时出错
- 数独解决方案仅适用于行和列
- Qt可以用于编写仅限命令行的应用程序吗
- 仅当数据从另一侧(c++)发送时,才从串行端口读取
- SDL:程序运行正常;仅绘制两行像素
- QHeaderView 仅在“调整大小到内容”模式下调整大小时考虑屏幕上的当前项目