C 中的动态矢量创建
Dynamic vector creation in C++
我正在尝试创建一个x数量的向量。在运行时间内将确定X的数量。也就是说,如果用户说他们需要2个向量,我们会创建两个向量,如果他们说他们需要3个,我们会创建3个向量等。在C 中执行此操作的最佳方法是什么?创建后我该如何使用这些方法?
假设您是指std::vector
,那么您问题的一种解决方案是使用向量的向量(无需双关语)。例如:
#include <iostream>
#include <vector>
int main()
{
// Create a vector containing vectors of integers
std::vector <std::vector<int>> v;
int X = 2; // Say you want 2 vectors. You can read this from user.
for(int i = 0; i < X; i++)
{
std::vector<int> n = {7, 5, 16, 8}; // or read them from user
v.push_back(n);
}
// Iterate and print values of vector
for(std::vector<int> n : v)
{
for(int nn : n )
std::cout << nn << 'n';
std::cout << std::endl;
}
}
相关文章:
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 创建动态对象并将其推送到矢量中
- 在创建动态二维数组时,在A[i*c+j]中使用c有什么用,即c代表什么
- 无法在 CMake 中创建动态库并将其链接到另一个动态库?
- 编写所需的代码以创建动态一维整数数组
- 创建动态分配的数组C
- 试图创建动态分配的char数组时的分割故障
- 在 c++ 上创建动态数组时出错
- 使用 MSVC C++编译器创建动态大小的数组
- 问题:在类中创建动态数组
- 如何在C 中创建动态数组,该数组将在执行时决定其大小
- 使用 Cmake 安装时创建动态库
- 为 C 绑定创建动态以 null 结尾的参数
- 如何在C 中创建动态分配的2D结构阵列
- 为矩阵创建动态数组,以循环迭代器命名
- 如何在C 中使用类创建动态列表
- 读取该文件,然后创建动态数组
- 包装本征的张量类以创建动态秩张量?
- 如何在C++中创建动态对象列表