简体   繁体   中英

Push_back a pointer of a vector

So I'm currently testing for a plan of mine. Here's what I'm currently doing. Basically I'm creating an array of structures that can expand infinitely. Now my problem is I want one of its vector to point to a location that is already part of the entire connections.

(This is actually an experiment for a neural network)

Now in the image below, this is how I'm going to use it. One of the items inside the vector (struct) will be pointed to another struct that is already part of base instead of just pushing and pushing. 在此处输入图片说明

#include<iostream>
#include<vector>

using namespace std;

//structure for each neurons
struct Test{
    char label[100]; 
    vector<Test> conn;
};

void main(){

vector<Test> base;
base.push_back(Test());
base[0].conn.push_back(Test());
strcpy(base[0].conn[0].label,"Test");

//test on pointer
Test test;
strcpy(test.label,"Test 2");

base[0].conn.push_back(test);
strcpy(test.label,"Test 3");
base[0].conn.push_back(test);

//push_back here base[0].conn[0]
//which contains "Test" text
//so that conn[0] and conn[3] points at the same thing

cout<<base[0].conn[0].label<<"\n";
cout<<base[0].conn[1].label<<"\n";
cout<<base[0].conn[2].label<<"\n";

}

** Extra: How can I, let's say the deepest part of the tree to be saved in a variable (its pointer?) so that I can easily edit it like deepest_part.label="something" instead of base.conn[0].conn 1 .conn[2] and so on?

This type of data structure is called a graph . That should be enough for you to find existing graph libraries (such as the Boost Graph Library ), or information on implementing your own.

Graphs can have a few types, yours is a directed graph, but allows cycles. Directed Acyclic Graphs (DAG) are common, useful, often-discussed, but won't work for you.

In order to allow cycles, you can't store nodes directly in their parents as you're trying - the child which creates a cycle would have to contain its own parent as a child, and this obviously breaks. You need to store pointers to nodes, if you use this scheme. The problem is that you can't trivially use smart pointers: shared_ptr will fail to deallocate cycles, and weak_ptr is only useful if you have some heuristic for deciding when a link should be strong and when it should be weak.

The much easier solution, and one used by the BGL linked above, is the adjacency list . Essentially all your nodes are stored in (and likely owned by) a table, and the conn vector just stores the indices of the connected nodes.

As for the deepest part of the tree, assuming you have some way to tell what that is (you'd have to visit every node, marking each with the length of the shortest path you found to reach it, detecting cycles, and have some way to resolve ties, etc.), you can just store the index. But, any alteration to the graph could change the value (eg, by adding or eliminating a shortcut to some subgraph), so you'd need to re-calculate it all the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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