- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,提出的问题是,
请看下面的代码——它是对动态数据集合进行操作的程序的骨架。
想法是使用一个结构包含两个字段:第一个存储集合中元素的数量,第二个是实际集合(一个动态的分配的整数 vector )。
如您所见,集合中填充了所需数量的伪随机数据。
不幸的是,该程序需要完成,因为最重要的功能(旨在将元素添加到集合中)仍然是空的。
这是我们对该函数的期望:
如果集合为空,它应该分配一个单元素 vector 并在其中存储一个新值;
如果集合不为空,则分配一个长度比当前 vector 大一的新 vector ,然后复制所有从旧 vector 到新 vector 的元素,将新值附加到新 vector ,最后释放旧 vector 。
我不希望得到解决方案或任何东西,但非常感谢指向正确方向的指示。
我已经对它进行了一段时间的修补,并设法让它至少使数组变大了一个,但它似乎只复制了第一个值的位置。
顺便说一句,我的代码只是 AddToCollection 下的东西。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct Collection {
int elno;
int *elements;
};
void AddToCollection(Collection &col, int element) {
// Insert your code here
if(col.elements != NULL) {
col.elno = sizeof(col.elements);
++col.elno;
int *jeffry = new int[col.elno + 1];
jeffry[col.elno] = element;
for (int f = 0; f < (col.elno - 1); f++) {
jeffry[f] = col.elements[f];
}
col.elements = jeffry;
}
if(col.elements == NULL){
col.elements =new int[element];
}
}
void PrintCollection(Collection col) {
cout << "[ ";
for(int i = 0; i < col.elno; i++)
cout << col.elements[i] << " ";
cout << "]" << endl;
}
int main(void) {
Collection collection = { 0, NULL };
int elems;
cout << "How many elements? ";
cin >> elems;
srand(time(NULL));
for(int i = 0; i < elems; i++)
AddToCollection(collection, rand() % 100 + 1);
PrintCollection(collection);
delete[] collection.elements;
return 0;
最佳答案
您正在到达那里,但您还没有完全得到的是 (1) col.elno = sizeof(col.elements);
是不变的,实际上等同于 col.elno = sizeof(a_pointer);
(这不是您想要的),以及 (2) 您必须在 AddToCollection
内处理两个互斥的条件。 .
col.elements
尚未分配,您只需分配一个 1
的数组并将第一个元素设置为 element
同时增加 col.elno
;和col.elements
之前已分配,您基本上必须 realloc (col.elements, ...)
使用 new
和 delete[]
.因为您必须复制旧的 col.elements
对于您在上面第二种情况中分配的新内存块,它有助于包含 <cstring>
提供memcpy
在这方面提供帮助。方法很简单。创建一个新的整数数组 col.elno + 1
元素,然后 memcpy
你现有的col.elements
到你之前的新街区delete[] col.elements
.然后只需将新的内存块分配给 col.elements
设置前 col.elements[col.elno++] = element;
.
你可以这样做:
void AddToCollection (Collection &col, int element)
{
if (!col.elements)
col.elements = new int[1];
else {
int *tmp = new int[col.elno + 1];
memcpy (tmp, col.elements, col.elno * sizeof *col.elements);
delete[] col.elements;
col.elements = tmp;
}
col.elements[col.elno++] = element;
}
注意:您应该始终使用内存错误检查程序(例如 valgrind
)来验证您的内存使用情况。对于 Linux。 (每个操作系统都有类似的检查器)。
然后你的程序变成:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
struct Collection {
int elno;
int *elements;
};
void AddToCollection (Collection &col, int element)
{
if (!col.elements)
col.elements = new int[1];
else {
int *tmp = new int[col.elno + 1];
memcpy (tmp, col.elements, col.elno * sizeof *col.elements);
delete[] col.elements;
col.elements = tmp;
}
col.elements[col.elno++] = element;
}
void PrintCollection(Collection col) {
cout << "[ ";
for(int i = 0; i < col.elno; i++)
cout << col.elements[i] << " ";
cout << "]" << endl;
}
int main(void) {
Collection collection = { 0, NULL };
int elems;
cout << "How many elements? ";
cin >> elems;
srand (time(NULL));
for (int i = 0; i < elems; i++)
AddToCollection (collection, rand() % 100 + 1);
PrintCollection(collection);
delete[] collection.elements;
return 0;
}
示例使用/输出
$ ./bin/dyncolelements
How many elements? 10
[ 1 21 26 24 57 26 99 86 12 23 ]
内存使用/错误检查
$ valgrind ./bin/dyncolelements
==11375== Memcheck, a memory error detector
==11375== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==11375== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==11375== Command: ./bin/dyncolelements
==11375==
How many elements? 10
[ 14 32 40 65 10 4 38 72 64 83 ]
==11375==
==11375== HEAP SUMMARY:
==11375== in use at exit: 0 bytes in 0 blocks
==11375== total heap usage: 11 allocs, 11 frees, 72,924 bytes allocated
==11375==
==11375== All heap blocks were freed -- no leaks are possible
==11375==
==11375== For counts of detected and suppressed errors, rerun with: -v
==11375== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
始终验证所有堆 block 都已释放,并且没有泄漏,也没有报告错误。
检查一下,如果您还有其他问题,请告诉我。
关于c++ - 实验 3.10.2 动态数据——如何获取和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54625266/
我正在测试 SQL,但我陷入了一个查询。这是一个无用的查询,但我想理解它。 select count(*), floor(rand()*2) as x from table_name group by
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
在我编写代码时,我经常喜欢查看代码库中特定区域的工作原理或某些表达式产生的结果。 大多数其他语言都提供了在运行时评估新的自定义表达式的工具。 Golang 似乎还没有提供这个功能,或者至少没有我使用过
1、kvm 简介 kernel-based virtual machine的简称,是一个开源的系统虚拟化模块,自linux 2.6.20之后集成在linux的各个主要发行版本中。它使用linux
我是中继新手,我正在尝试在并发模式下使用中继现代实验。我已经能够使用 Suspense 和 ErrorBoundary 很好地加载节点、边等。我现在正在处理用于创建和更新对象的表单。 我无法弄清楚如何
我正在参加在线软件安全类(class)。我正在尝试使用 shellcode 进行实验。我编写了一个易受攻击的服务器、一个注入(inject)程序、一个(可能已损坏的)shellcode,我将其转换为程
我最糟糕的噩梦是日期对象,所以我创建了一个 fiddle 来查看它是如何工作的,并尝试为以下格式的 date strings 找到解决方案 2015-10- 05T11:49:13.587Z 但要注意
我一直在研究 Accessibility Object Model API,我认为尝试在 Puppeteer 测试中使用它会很酷。 getComputedAccessibleNode 返回一个 pro
我目前正在概述 C++11 的新特性,由于目前不明原因,其中一些特性无法编译。我使用 gcc version 4.6.0 20100703 (experimental) (GCC) 所以根据 GNU
1.动态年龄判定规则 对象进入老年代的4个常见的时机: 1、 躲过15次gc,达到15岁高龄之后进入老年代; 2、 动态年龄判定规则,如果Survivor区域内年龄1+年龄2+年龄3+年龄n的对象
有没有办法停止ray.tune实验(例如使用 PBT)当明显过度拟合或一个指标长时间没有改善时? 最佳答案 现在,这在 Tune 中得到了很好的支持,https://github.com/ray-pr
我尝试在gcc 6.0的开发中实际使用新的c++ 1z功能。 如果我尝试这个小例子: #include #include namespace fs = std::experimental::fil
我想知道为什么我在服务器端运行实验时必须包含 JavaScript cxApi。此外,我可以通过 PHP 发送选定的实验和变体吗?或者可能通过在没有外部资源(如 cxApi)的情况下注入(inject
我正在开发一个使用 Firebase iOS SDK 的 iOS 应用,我正在尝试弄清楚如何访问用户已看到的所有 Firebase AB 实验以及用户参与的实验变体。 我查看了文档以及 Firebas
我用 python 2.7.7 运行了以下命令: import gc import memory_profiler print memory_profiler.memory_usage()[0] x
我在浏览器控制台中做了一个 JavaScript 实验 - 首先我创建了一个新对象 foo 如下 var foo = { bar: function() { return this.baz
据说“4916 个正面训练示例经过手工挑选对齐、归一化并缩放到 24x24 的基本分辨率。通过从 9500 张不包含人脸的图像中随机挑选子窗口来选择 10,000 个负面示例。”在论文“Paul Vi
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Can you write object oriented code in C? 嗨! 只是为了好玩,这两天
1. 程序的JVM参数示范 已知,平时系统运行创建的对象,除非是那种大对象,否则通常来说都是优先分配在新生代中的Eden区域的。 而且新生代还有另外两块Survivor区域,默认Eden区域占据新
在 上创建新实验DAGsHub 使用Git,".._metrics.csv"的格式应该是什么和 ".._params.yml"文件? 不幸的是,我在任何地方都找不到引用。 最佳答案 特尔;博士: 对于
我是一名优秀的程序员,十分优秀!