简体   繁体   English

具有自己成员的成员Vector的类

[英]Class that has the member Vector of its own members

I'm trying to write a Neural Network. 我正在尝试编写一个神经网络。 I've done it before in other languages, and using matrices libraries; 我以前使用其他语言并使用矩阵库完成过此操作; however, I wanted to make it on my own, to better understand them. 但是,我想靠自己做,以更好地了解他们。 However I have run into a problem with my classes. 但是我的课遇到了问题。

I am doing this in C++. 我在C ++中执行此操作。

I have a class called ANN. 我有一堂课叫ANN。 It has a vector of layers. 它具有图层向量。 layers are a class called Node. 图层是称为Node的类。 However, within node I want to access the previous layer of nodes, so I can calculate this node's values. 但是,在节点内,我想访问节点的上一层,因此我可以计算该节点的值。

I know if I have a class, and I want one of its members to be of itself it has to be a pointer. 我知道我是否有一个类,并且我希望它的一个成员本身就是一个指针。 ex. 恩。

class Node{
public:
    // Public methods/members here
private:
    Node *previousNode;
}

However, this is not what I want. 但是,这不是我想要的。 I want *previousNode to be ancestor of nodes 我希望*previousNode成为节点的祖先

here is an example of what I want 这是我想要的一个例子

class Node{
public:
    //Public functions/members here
private:
    vector <Node*> previousLayer;
}

Now I've heard in this situation it would be better to use a smart pointer. 现在,我听说在这种情况下,使用智能指针会更好。 I don't exactly know what that is, I know it is basically a wrapper for pointers that manage them. 我不完全知道这是什么,我知道它基本上是管理它们的指针的包装。 I also thought the main reason smart pointers were used is because some pointers get left NULL or not deleted after use, but I did not think that would really matter seeing as the scope of this class could only possible end when the program ends. 我还认为使用智能指针的主要原因是因为某些指针在使用后变为NULL或未删除,但我认为这并不重要,因为此类的作用域只能在程序结束时结束。

My question is how would I implement a vector of pointers to the class the vector is in. 我的问题是如何实现指向该向量所在类的指针的向量。

If I'm interpreting your question correctly, it sounds like you want each instance of Node to contain a list of pointers to other Node objects that it is connected to. 如果我正确地解释了您的问题,听起来您希望每个Node实例都包含一个指向与其连接的其他Node对象的指针的列表。

What you have written is fine, but as you say, it could make for safer code to use std::shared_ptr . 您所写的内容很好,但是正如您所说,使用std::shared_ptr可以使代码更安全。

class Node{
 public:
     //Public functions/members here
 private:
     vector <shared_ptr<Node>> previousLayer;
 }

You should allocate your nodes with std::make_shared and they will persist in memory for as long as they are used (no need for new or delete .) 您应该为节点分配std::make_shared并且它们将在内存中持续使用的时间(不需要使用newdelete

std::vector <Node*> previousLayer;

if you want to handle all the element in the vector, using the iterator: 如果要使用迭代器处理向量中的所有元素,请执行以下操作:

for (auto it = previousLayer.begin(); it != previousLayer.end(); ++it)
{
    Node* node = *it;
    handle(node);
}

// Note: you can using "for (std::vector<Node*>::iterator it = previousLayer.begin(); it != previousLayer.end(); ++it)" instead.

if you only want to access one element, using the operator []: 如果您只想访问一个元素,请使用运算符[]:

Node* node = previousLayer[n];
handle(node);
// but be careful, don't make the n out of range. that means n must be "0 <= n < previousLayer.size()"

vector introduction: http://en.cppreference.com/w/cpp/container/vector 矢量介绍: http : //en.cppreference.com/w/cpp/container/vector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM