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. How to make global 2D array using vectors

How to make global 2D array using vectors

Scheduled Pinned Locked Moved C / C++ / MFC
c++graphicsdata-structurestutorial
6 Posts 3 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"

    #include

    using namespace std;

    extern int row;
    extern int col;

    extern vector> _muPop(row, vector(col));

    class ESSolver
    {
    public:
    ESSolver();
    void Init();
    void Disp();

    private:
    	vector< vector > \*muPop;
    

    };

    "ESSolver.cpp"

    #include
    #include
    #include "../include/ESSolver.h"

    using namespace std;

    ESSolver::ESSolver()
    {
    muPop = &_muPop;

    return;
    

    }

    void ESSolver::Init()
    {
    // Initializing object variable
    for (int i = 0; i < row; i++)
    {
    for (int j = 0; j < col; j++)
    {
    (*muPop)[i][j] = i+1;
    }
    }
    }

    void ESSolver::Disp()
    {
    // Initializing object variable
    for (int i = 0; i < row; i++)
    {
    for (int j = 0; j < col; j++)
    {
    cout << (*muPop)[i][j] << " ";
    }
    cout << endl;
    }
    }

    "main.cpp"

    #include "../include/ESSolver.h"
    #include #include using namespace std;

    int row;
    int col;

    vector> _muPop(row, vector(col));

    int main(int argc, char *argv[]){
    //./main #row #col

    row = atoi(argv\[1\]);
    col = atoi(argv\[2\]);
    
    ESSolver es;
    es.Init();
    

    }

    L J CPalliniC 3 Replies Last reply
    0
    • L Lost User

      I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"

      #include

      using namespace std;

      extern int row;
      extern int col;

      extern vector> _muPop(row, vector(col));

      class ESSolver
      {
      public:
      ESSolver();
      void Init();
      void Disp();

      private:
      	vector< vector > \*muPop;
      

      };

      "ESSolver.cpp"

      #include
      #include
      #include "../include/ESSolver.h"

      using namespace std;

      ESSolver::ESSolver()
      {
      muPop = &_muPop;

      return;
      

      }

      void ESSolver::Init()
      {
      // Initializing object variable
      for (int i = 0; i < row; i++)
      {
      for (int j = 0; j < col; j++)
      {
      (*muPop)[i][j] = i+1;
      }
      }
      }

      void ESSolver::Disp()
      {
      // Initializing object variable
      for (int i = 0; i < row; i++)
      {
      for (int j = 0; j < col; j++)
      {
      cout << (*muPop)[i][j] << " ";
      }
      cout << endl;
      }
      }

      "main.cpp"

      #include "../include/ESSolver.h"
      #include #include using namespace std;

      int row;
      int col;

      vector> _muPop(row, vector(col));

      int main(int argc, char *argv[]){
      //./main #row #col

      row = atoi(argv\[1\]);
      col = atoi(argv\[2\]);
      
      ESSolver es;
      es.Init();
      

      }

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

      You declare your vector as a global, but the values of row and col will be zero when it is created. Make it a local variable and let your ESSolver object manage it. Or better still make it a member of the ESSolver class.

      1 Reply Last reply
      0
      • L Lost User

        I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"

        #include

        using namespace std;

        extern int row;
        extern int col;

        extern vector> _muPop(row, vector(col));

        class ESSolver
        {
        public:
        ESSolver();
        void Init();
        void Disp();

        private:
        	vector< vector > \*muPop;
        

        };

        "ESSolver.cpp"

        #include
        #include
        #include "../include/ESSolver.h"

        using namespace std;

        ESSolver::ESSolver()
        {
        muPop = &_muPop;

        return;
        

        }

        void ESSolver::Init()
        {
        // Initializing object variable
        for (int i = 0; i < row; i++)
        {
        for (int j = 0; j < col; j++)
        {
        (*muPop)[i][j] = i+1;
        }
        }
        }

        void ESSolver::Disp()
        {
        // Initializing object variable
        for (int i = 0; i < row; i++)
        {
        for (int j = 0; j < col; j++)
        {
        cout << (*muPop)[i][j] << " ";
        }
        cout << endl;
        }
        }

        "main.cpp"

        #include "../include/ESSolver.h"
        #include #include using namespace std;

        int row;
        int col;

        vector> _muPop(row, vector(col));

        int main(int argc, char *argv[]){
        //./main #row #col

        row = atoi(argv\[1\]);
        col = atoi(argv\[2\]);
        
        ESSolver es;
        es.Init();
        

        }

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        Your concept is totally wrong and won't work at least here:

        vector> _muPop(row, vector(col));

        because row and col are not defined. Write a class that holds the 2D array. That class should have an initialisation function with the number of rows and columns as parameters that allocates the array and initialises the array items. Then there is even no need to have a global instance of that class. It can be a local member of your main function. You might also check if the usage of vector is really necessary. If you don't need vector specific operations using classic C style arrays or C++ array templates might be a better option.

        1 Reply Last reply
        0
        • L Lost User

          I am trying to make a global 2D array using vectors, but I am having some trouble in doing so. Below code is a small part of my project where I need to make a global 2D array whose size has to be passed through a command line. I am passing row and column as command line arguments. "ESSolver.h"

          #include

          using namespace std;

          extern int row;
          extern int col;

          extern vector> _muPop(row, vector(col));

          class ESSolver
          {
          public:
          ESSolver();
          void Init();
          void Disp();

          private:
          	vector< vector > \*muPop;
          

          };

          "ESSolver.cpp"

          #include
          #include
          #include "../include/ESSolver.h"

          using namespace std;

          ESSolver::ESSolver()
          {
          muPop = &_muPop;

          return;
          

          }

          void ESSolver::Init()
          {
          // Initializing object variable
          for (int i = 0; i < row; i++)
          {
          for (int j = 0; j < col; j++)
          {
          (*muPop)[i][j] = i+1;
          }
          }
          }

          void ESSolver::Disp()
          {
          // Initializing object variable
          for (int i = 0; i < row; i++)
          {
          for (int j = 0; j < col; j++)
          {
          cout << (*muPop)[i][j] << " ";
          }
          cout << endl;
          }
          }

          "main.cpp"

          #include "../include/ESSolver.h"
          #include #include using namespace std;

          int row;
          int col;

          vector> _muPop(row, vector(col));

          int main(int argc, char *argv[]){
          //./main #row #col

          row = atoi(argv\[1\]);
          col = atoi(argv\[2\]);
          
          ESSolver es;
          es.Init();
          

          }

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          As suggested, don't use globals. Try, for instance

          #include #include #include #include using namespace std;

          class Matrix
          {
          int R, C;
          vector > m;
          public:
          Matrix(int r, int c):R(r),C(c)
          {
          for (int r = 0; r());
          for (int c = 0; c= 3)
          {
          rows = stoi(argv[1]);
          cols = stoi(argv[2]);
          }

          Matrix m(rows,cols);
          cout << m << endl;
          }

          In testa che avete, signor di Ceprano?

          L 1 Reply Last reply
          0
          • CPalliniC CPallini

            As suggested, don't use globals. Try, for instance

            #include #include #include #include using namespace std;

            class Matrix
            {
            int R, C;
            vector > m;
            public:
            Matrix(int r, int c):R(r),C(c)
            {
            for (int r = 0; r());
            for (int c = 0; c= 3)
            {
            rows = stoi(argv[1]);
            cols = stoi(argv[2]);
            }

            Matrix m(rows,cols);
            cout << m << endl;
            }

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

            Thanks

            CPalliniC 1 Reply Last reply
            0
            • L Lost User

              Thanks

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You are welcome.

              In testa che avete, signor di Ceprano?

              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