C++:使用 for 循环创建多个数据结构
C++: creating multiple data structures using a for loop
我有一个程序,我在其中使用以下形式的记录:
// declaring a struct for each record
struct record
{
int number; // number of record
vector<int> content; // content of record
};
在 main 中,我声明每条记录:
record batch_1; // stores integers from 1 - 64
record batch_2; // stores integers from 65 - 128
其中,每个批次存储数字列表中的 64 个整数(在本例中来自总共 128 个数字的列表(。我想使这个程序开放,以便该程序能够处理任何列表大小(约束它是 64 的倍数(。因此,如果列表大小为 256,我将需要四条记录(batch_1 - batch_4(。我不确定如何创建 N 条记录,但我正在寻找这样的东西(这显然不是解决方案(:
//creating the batch records
for (int i = 1; i <= (list_size / 64); i++)
{
record batch_[i]; // each batch stores 64 integers
}
如何做到这一点,在 for 循环中声明的内容的范围是否会超出循环本身?我想数组可以满足范围要求,但我不确定如何实现它。
就像评论中的许多人建议的那样,为什么不使用C++标准库提供的可调整大小的向量:std::vector
?
因此,与其拥有这个:
record batch_1; // stores integers from 1 - 64
record batch_2; // stores integers from 65 - 128
.
.
record batch_n // Stores integers x - y
替换为:
std::vector<record> batches;
//And to create the the batch records
for (int i = 1; i <= (list_size / 64); i++) {
record r;
r.number = i;
r.content = ....;
batches.push_back(r);
// You could also declare a constructor for your record struct to facilitate instantiating it.
}
你为什么不试试这个
// code
vector<record> v(list_size / 64);
// additinal code goes here
现在,您可以按如下方式访问您的数据
(v[i].content).at(j);
相关文章:
- 链表,反向函数,数据结构
- 尝试创建排序的动态列表(数据结构)
- 如何读取文件中的数据并创建结构向量
- C++:使用 for 循环创建多个数据结构
- 如何在 C++ 中创建抽象类的对象或数据结构?
- 如何创建辅助数据结构来跟踪 c++ 中 decrease_key 操作的堆索引
- 是否值得两次通过文件进行迭代,以创建最小的数据结构
- 如何为我的 Vulkan 类创建此通用数据结构字段
- 在创建C 枚举和依赖数据结构时,如何避免重复自己
- 用于创建、可视化和算法操作数据结构的C++库
- 在C++中创建自定义的类似树的数据结构器
- 我可以读取输入数据并创建广义C++数据结构吗?
- 如何从 CGAL 中的坐标和拓扑列表创建Polyhedron_3数据结构
- 如何在c++中动态创建一个具有列表底层数据结构的队列
- 如何创建/设计数据结构
- 创建一个树数据结构-不同的方法
- 创建一个由链接平衡最佳表和双链表组成的数据结构
- 无法创建数组大小 [300][300] 的二维数据结构 - >堆栈溢出
- 线程分叉创建数据结构
- 如何在c++中创建树形数据结构