在类中创建指向向量的指针

发布于 2024-12-12 05:01:42 字数 683 浏览 0 评论 0原文

我正在创建一个 CAD 软件,其中有一个类 point &类线

#include <vector>
using namespace std;
class point
{
private:
    vector<int>* child;
    int id;//this is unique id of a point
    double x;//absolute x, y, z co-ord
    double y;//
    double z;//
};

class line
{
private:
    point *a;
    point *b;
    int id;//this is unique for each line object
}

任何线对象都是 2 个点对象的子对象。 因此,如果我删除一条线的一个点,那么该线对象也应该被删除。 为此,我想将点对象的所有子对象(线、圆、三角形……)的id存储在向量child(类的实例变量)中点如代码所示)。

我的问题是我的方法正确吗? 我的意思是,一次执行中将有大约 100 个点对象。 由于每个向量分配一些额外的内存,因此将有大量分配的内存不会在执行中使用。 任何人都可以建议另一种方法将未知的 int 存储为每个点对象中的序列吗?

I am creating a CAD software in which I have a class point & a class line.

#include <vector>
using namespace std;
class point
{
private:
    vector<int>* child;
    int id;//this is unique id of a point
    double x;//absolute x, y, z co-ord
    double y;//
    double z;//
};

class line
{
private:
    point *a;
    point *b;
    int id;//this is unique for each line object
}

any line object is the child of 2 point objects.
so if i delete a point of a line then the line object shud also be deleted.
For this i want to store the id of all children (line, circle, triangle,....) of a point object in a vector child (instance variable of class point as shown in code).

my question is is my approach correct?
i mean that, there will be around 100's of point objects in an execution.
since each vector allocates some extra memory, there will be a lot of allocated memory which will not be used in an execution.
can anybody suggest an alternative way of storing unknown no of int as a sequence in each point object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不可一世的女人 2024-12-19 05:01:42

您不需要使用指向向量的指针,对象就可以:

#include <vector>
class point
{
private:
   std::vector<int> child;
   int id;//this is unique id of a point
   double x;//absolute x, y, z co-ord
   double y;//
   double z;//
};

不要忘记包含 并使用 std::vector 限定您的使用>。

You don't need to use a pointer to a vector, an object would do:

#include <vector>
class point
{
private:
   std::vector<int> child;
   int id;//this is unique id of a point
   double x;//absolute x, y, z co-ord
   double y;//
   double z;//
};

Don't forget to include <vector> and qualify your use with std::vector.

梦行七里 2024-12-19 05:01:42

我的问题是如何创建这个向量&在哪里(这样它就不会超出范围)?

您已经正确完成了此操作。我建议您不要使用指向向量的指针,而只是使用向量本身:

private:
    vector<int> child;

这使得处理它变得更容易,并且您不必分配/deallocete 自己。


如何让我的孩子指向创建的向量而不发生内存泄漏?

如果您确实需要动态分配的指针,则必须调用newdelete。为了使其正常运行,您必须在 point 类中定义构造函数和析构函数:

class point
{
private:
    point() : child(new vector<int>) {}
    ~point() { delete child; }

    vector<int>* child;
    int id;//this is unique id of a point
    double x;//absolute x, y, z co-ord
    double y;//
    double z;//
};

但我仍然建议您不要使用指向 vector 的指针正如我上面所解释的。

my question is how do i create this vector & where (so that it doesnt go out of scope)?

You have already done this correctly. I'd suggest that you dont't use a pointer to your vector but simply a vector itself:

private:
    vector<int> child;

This makes handling it much easier, and you don't have to allocate/deallocete it by yourself.


how do i make my child point to the created vector without memory leak?

If you really need to have a dynamically allocated pointer, you have to call new and delete. For this to behave correctly, you have to define both a constructor and a destructor in your point class:

class point
{
private:
    point() : child(new vector<int>) {}
    ~point() { delete child; }

    vector<int>* child;
    int id;//this is unique id of a point
    double x;//absolute x, y, z co-ord
    double y;//
    double z;//
};

But I still recommend that you don't use a pointer to a vector as I have explained above.

木有鱼丸 2024-12-19 05:01:42

我会用完全不同的方式解决这个问题。

我会按 id 保留两个全局哈希表:一个用于点,一个用于线。
然后我将为每个点使用双向链表数据结构,以便您知道有多少行正在使用给定点。

我将通过全局容器类中的简单操作来轻松访问这些数据结构。

我将尝试编写一些“伪”代码以使其更清晰(很快编辑)。

I would solve the problem in a totally different way.

I would keep two global hashtables by id: one for points and one for lines.
Then i would use a doubly linked list data structure for each point so that you know how many lines are using a given point.

I would make the access to these data structure easy through simple operations in a global container class.

I'll try to write some "pseudo" code to make it clearer (editing soon).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文