I have read the topic about "pointer", but i still have some question.
// graph.cpp
struct Edge {
int from;
int to;
unsigned int id;
Edge(): from(0), to(0), id(0) {};
};
struct Vertex {
int label;
vector<Edge> edge;
};
class Graph: public vector<Vertex> {
int gid;
unsigned int edge_size;
};
if I declare a iterator in another file
bool get_forward_root (Graph &g, Vertex &v, vector<Edge*> &result) {
for(vector<Edge>::iterator it = v.edge.begin(); it != v.edge.end(); it++) {
if(v.label <= g[it->to].label)
result.push_back(&(*it));
}
}
In my understanding, it
can be viewed as pointer, since v.edge.begin()
is the first Edge
object in vector<Edge>
, but what is &(*it)
?
Question 2. What is the difference between g
, &g
, *g
?
In my understanding:
&g
is the memory address.*g
is a Graph pointer point to a graph object, so we can use Graph *g = new Graph();g
is a Graph object
the difference between *g and g is how we use, for example the two conditions are the same:
condition 1:
Graph *g = new Graph();
g->gid = 0;
condition 2:
Graph g;
g.gid = 0;
Question 3.
what is below meaning?
Graph &g
and why we use g[it->to].label
not &g[it->to].label
Thank very much:)