在类中创建指向向量的指针
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要使用指向向量的指针,对象就可以:
不要忘记包含
并使用std::vector
限定您的使用>。You don't need to use a pointer to a vector, an object would do:
Don't forget to include
<vector>
and qualify your use withstd::vector
.您已经正确完成了此操作。我建议您不要使用指向向量的指针,而只是使用向量本身:
这使得处理它变得更容易,并且您不必分配/deallocete 自己。
如果您确实需要动态分配的指针,则必须调用
new
和delete
。为了使其正常运行,您必须在point
类中定义构造函数和析构函数:但我仍然建议您不要使用指向
vector
的指针正如我上面所解释的。You have already done this correctly. I'd suggest that you dont't use a pointer to your
vector
but simply avector
itself:This makes handling it much easier, and you don't have to allocate/deallocete it by yourself.
If you really need to have a dynamically allocated pointer, you have to call
new
anddelete
. For this to behave correctly, you have to define both a constructor and a destructor in yourpoint
class:But I still recommend that you don't use a pointer to a
vector
as I have explained above.我会用完全不同的方式解决这个问题。
我会按 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).