gpt4 book ai didi

c++ - 实验 3.10.2 动态数据——如何获取和删除

转载 作者:行者123 更新时间:2023-11-30 03:18:33 24 4
gpt4 key购买 nike

所以,提出的问题是,

请看下面的代码——它是对动态数据集合进行操作的程序的骨架。

想法是使用一个结构包含两个字段:第一个存储集合中元素的数量,第二个是实际集合(一个动态的分配的整数 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 内处理两个互斥的条件。 .

  1. col.elements尚未分配,您只需分配一个 1 的数组并将第一个元素设置为 element同时增加 col.elno ;和
  2. col.elements之前已分配,您基本上必须 realloc (col.elements, ...)使用 newdelete[] .

因为您必须复制旧的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com