[英]C++ Dynamic data – how to obtain it and how to get rid of it
The code below – it's a skeleton of a program operating on the dynamic collection of data. 下面的代码–是动态处理数据集合的程序的框架。 The idea is to use a structure containing two fields: the first stores the number of elements in collections, and the second is the actual collection (a dynamically allocated vector of ints). 这个想法是使用包含两个字段的结构:第一个存储集合中元素的数量,第二个是实际集合(动态分配的ints向量)。 As you can see, the collection is filled with the required amount of pseudo-random data. 如您所见,集合中填充了所需数量的伪随机数据。 Unfortunately, the program requires completion, as the most important function. 不幸的是,该程序要求完成是最重要的功能。
Here's what i expect from the function: 这是我对功能的期望:
if the collection is not empty, it should allocate a new vector with a length greater by one than the current vector, then copy all elements from the old vector to the new one, append a new value to the new vector and finally free up the old vector. 如果集合不为空,则应分配一个长度比当前向量大一的新向量,然后将所有元素从旧向量复制到新向量,将新值附加到新向量,最后释放旧的向量。
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; struct Collection { int elno; int *elements; }; void AddToCollection(Collection &col, int element) { //the first part of the funtion if (col.elno==0){ col.elements= new int[1]; col.elements[0]= element; } //this is the second part but i do not know how to do it. //Please help me to complete*************** else { int *temp; temp = new[]; } } 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; }
vector container is originally dynamic container. 向量容器最初是动态容器。 so u can use vector. 所以你可以使用向量。
Just declare vector variable in structure and use it in AddToCollection
function. 只需在结构中声明矢量变量,然后在AddToCollection
函数中使用它AddToCollection
。
struct Collection {
int elno;
std::vector<int> elements;
};
void AddToCollection(Collection &col, int element) {
col.elements.push_back(element);
col.elno++;
}
like this. 像这样。
Here is what you are looking for: 这是您要寻找的:
void AddToCollection(Collection &col, int element)
{
if(col.elements == NULL)
{
col.elements = new int[1];
col.elements[0] = element;
col.elno = 1;
}
else
{
int *newArr = new int[col.elno+1];
for(int i = 0; i < col.elno; i++)
{
newArr[i] = col.elements[i];
}
newArr[col.elno] = element;
delete[] col.elements;
col.elements = new int[col.elno+1];
for(int i = 0; i < col.elno+1; i++)
{
col.elements[i] = newArr[i];
}
delete[] newArr;
newArr = NULL; // avoid dangling pointer
col.elno++;
}
}
For sure using vector container is a great ideea but the exercise require no modification to the main function. 当然,使用向量容器是一个不错的主意,但此练习不需要修改主要功能。 The objective of this exercise is to help the student to understand dynamically allocated memory . 该练习的目的是帮助学生理解动态分配的内存 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.