C ++动态数据 - 如何获取它以及如何摆脱它 [英] C++ dynamic data – how to obtain it and how to get rid of it

查看:144
本文介绍了C ++动态数据 - 如何获取它以及如何摆脱它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码 - 它是运行动态数据集的程序的框架。我们的想法是使用包含两个字段的结构:第一个存储集合中的元素数量,第二个是实际集合(动态分配的int向量)。正如您所看到的,该集合中填充了所需数量的伪随机数据。

不幸的是,程序需要完成,这是最重要的功能。

这是什么我期望从函数:

1.如果集合为空,它应该分配一个单元素向量并在其中存储一个新值。

2.如果集合不是空的,它应该分配一个长度比当前向量大1的新向量,然后将旧向量中的所有元素复制到新向量,将新值附加到新向量,最后释放旧向量。



我尝试了什么:



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). 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:
1. if the collection is empty, it should allocate a one-element vector and store a new value in it.
2. 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.

What I have tried:

#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


}

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;
}

推荐答案

Quote:

int * temp;

temp = new [];

int *temp;
temp = new[];

让我们来解决你的起点。上面的statemens应该是

Let's fix your starting point. The above statemens should be

int * temp;
temp = new int[col.elno + 1];

现在你的 temp 向量作为正确的项目数。



你必须

  • 复制所有 col.elements 项目进入 temp
  • 元素 > temp 。
  • 发布 col.elements 内存( delete 它)。
  • 设置 col.elements 等于 temp
  • 增量 col.elno
  • Now your temp vector as the right number of items.

    You have to

    • Copy all the col.elements items into temp.
    • Add element at the end of temp.
    • Release col.elements memory (delete it).
    • Set col.elements equal to temp.
    • Increment col.elno

    • As我告诉过你今天早些时候发布这个消息时,我们不会为你工作。删除问题并重新发布,希望我们不会注意到这不是一个好主意 - 它不会让你的工作完成,但它会让人烦恼。



      仔细阅读说明书,并考虑您的需求。你可以很容易地在你浪费时间浪费这个问题时完成这个......
      As I told you when you posted this earlier today, we do not do your work for you. Deleting the question and reposting it in the hope we won't notice is not a good idea - it won't get your work done for you, but it will annoy people.

      Read the instructions carefully, and think about what you need. You could easily have completed this in the time you have wasted posting this question repeatedly...


      这篇关于C ++动态数据 - 如何获取它以及如何摆脱它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆