C++ Vector object question
-
Hi, I read a block of code, like:
class Graph
{
public:
int V; // number of vertices
vector *adj; //adjacency listGraph(int V); void addEdge(int x, int y); bool isRoute(int x, int y);
};
// Constructor
Graph::Graph(int V)
{
this->V = V;
this->adj = new vector[V];
}// add a directed edge from x to y
void Graph::addEdge(int x, int y) {
adj[x].push_back(y);
}int main() {
Graph g(6); g.addEdge(5, 2); g.addEdge(5, 0); g.addEdge(4, 0); g.addEdge(4, 1); g.addEdge(2, 3); g.addEdge(3, 1);
}
I wonder the adj is a pointer that points to a Vector object, and a vector object I think it's like an one-dimensional array, but the addEdge() operation, adj[x].push_back(y),it make another array[x,y]. in Main(), the structure of the vector object, seems like: [5,2,0] [4,0,1] [2,3] [3,1] Then this is not an one-dimensional array.so this is a Vector object, or 4 vector objects? Thanks
-
Hi, I read a block of code, like:
class Graph
{
public:
int V; // number of vertices
vector *adj; //adjacency listGraph(int V); void addEdge(int x, int y); bool isRoute(int x, int y);
};
// Constructor
Graph::Graph(int V)
{
this->V = V;
this->adj = new vector[V];
}// add a directed edge from x to y
void Graph::addEdge(int x, int y) {
adj[x].push_back(y);
}int main() {
Graph g(6); g.addEdge(5, 2); g.addEdge(5, 0); g.addEdge(4, 0); g.addEdge(4, 1); g.addEdge(2, 3); g.addEdge(3, 1);
}
I wonder the adj is a pointer that points to a Vector object, and a vector object I think it's like an one-dimensional array, but the addEdge() operation, adj[x].push_back(y),it make another array[x,y]. in Main(), the structure of the vector object, seems like: [5,2,0] [4,0,1] [2,3] [3,1] Then this is not an one-dimensional array.so this is a Vector object, or 4 vector objects? Thanks
-
focusdoit wrote:
I wonder the adj is a pointer that points to a Vector object
No, it's an array of vectors with V elements. So it's a two-dimensional data structure.
-
Yes, it's a pointer to a vector, but you are trying to assign an array of vectors to it. Why not use the suggestion I offered above?
-
focusdoit wrote:
I wonder the adj is a pointer that points to a Vector object
No, it's an array of vectors with V elements. So it's a two-dimensional data structure.
-
Thanks, I am reading the program. Just try to understand why a pointer to an array, be used as an array of vectors.
It's not a pointer to an array, it's a pointer to a vector (singular). If you want a pointer to an array of vectors then you need something like:
vector *varray[];
Then each element of
varray
will need to be avector*
, that is, a pointer to a vector. A vector is an instance of the vector class so each pointer will point to a single vector. -
is it similar to a pointer that points to an integer. like: int a = 0; int *ip = &a; then (ip+1) point to an integer too. but do we need to allocate memory space to it?
Yes, you must always allocate memory if you intend to read from or write to it. A pointer declaration does not allocate any memory, it is just a single variable that can be used to address individual items. But the memory space to hold the items must be allocated before you use the pointer.
-
is it similar to a pointer that points to an integer. like: int a = 0; int *ip = &a; then (ip+1) point to an integer too. but do we need to allocate memory space to it?