Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Vector object question

C++ Vector object question

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresquestionc++graphics
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    focusdoit
    wrote on last edited by
    #1

    Hi, I read a block of code, like:

    class Graph
    {
    public:
    int V; // number of vertices
    vector *adj; //adjacency list

    Graph(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

    L M 2 Replies Last reply
    0
    • F focusdoit

      Hi, I read a block of code, like:

      class Graph
      {
      public:
      int V; // number of vertices
      vector *adj; //adjacency list

      Graph(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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Not sure what any of that means. But why not use a POINT structure (or create your own), and then you can have a vector of points?

      struct POINT
      {
      int x;
      int y;
      };
      vector pointList;

      1 Reply Last reply
      0
      • F focusdoit

        Hi, I read a block of code, like:

        class Graph
        {
        public:
        int V; // number of vertices
        vector *adj; //adjacency list

        Graph(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

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        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.

        S 2 Replies Last reply
        0
        • M markkuk

          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.

          S Offline
          S Offline
          samzcs
          wrote on last edited by
          #4

          vector *adj; //adjacency list is it point to an Vector object?

          L 1 Reply Last reply
          0
          • S samzcs

            vector *adj; //adjacency list is it point to an Vector object?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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?

            S 1 Reply Last reply
            0
            • L Lost User

              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?

              S Offline
              S Offline
              samzcs
              wrote on last edited by
              #6

              Thanks, I am reading the program. Just try to understand why a pointer to an array, be used as an array of vectors.

              L 1 Reply Last reply
              0
              • M markkuk

                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.

                S Offline
                S Offline
                samzcs
                wrote on last edited by
                #7

                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?

                L M 2 Replies Last reply
                0
                • S samzcs

                  Thanks, I am reading the program. Just try to understand why a pointer to an array, be used as an array of vectors.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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 a vector*, 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.

                  1 Reply Last reply
                  0
                  • S samzcs

                    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?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • S samzcs

                      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?

                      M Offline
                      M Offline
                      markkuk
                      wrote on last edited by
                      #10

                      Memory is allocated in the constructor:

                      this->adj = new vector[V];

                      S 1 Reply Last reply
                      0
                      • M markkuk

                        Memory is allocated in the constructor:

                        this->adj = new vector[V];

                        S Offline
                        S Offline
                        samzcs
                        wrote on last edited by
                        #11

                        Thanks, I understood

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups