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. Return bidimensional array

Return bidimensional array

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicsdata-structures
13 Posts 6 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.
  • _ _Flaviu

    I have a class (taken from somewhere). Here, I have a member like this:

    public:
    void SetBitmapDataSize(const int x, const int y);

    private:
    int m_PixelNumberX;
    int m_PixelNumberY;
    vector > BitmapData;

    and SetBitmapDataSize is implemented:

    void CBitmapOp::SetBitmapDataSize(const int x, const int y)
    {
    m_PixelNumberX = x;
    m_PixelNumberY = y;
    if((x>=0) && (y>=0))
    {
    BitmapData.resize(m_PixelNumberX);
    for(int x = 0; x < m_PixelNumberX; x++)
    BitmapData[x].resize(m_PixelNumberY);
    }
    }

    Now, I want to replace vector > with this CArray: I had tried:

    private:
    int m_PixelNumberX;
    int m_PixelNumberY;
    CArray< CArray, CArray > m_arrBitmapData;

    my question is, how can replace vector.resize method, and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ? Thank you.

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #3

    Quote:

    Now, I want to replace vector > with this CArray:

    That's not advisable. I wouldn't do it. As matter of fact the vector container is much better than the CArray one.

    Quote:

    how can replace vector.resize method,

    Use the Carray::SetSize[^] method.

    Quote:

    how can I return CArray< CArray, CArray > data type

    The same way you return other data, e.g.

    return m_arrBitmapData;

    THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

    1 Reply Last reply
    0
    • D David Crow

      Flaviu2 wrote:

      my question is, how can replace vector.resize method...

      Have you tried this.

      Flaviu2 wrote:

      ...and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ?

      From where?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #4

      Ok, resize issue has been solved. Secondly, as long m_arrBitmapData is private memeber, I want to return this memeber through a public method, I mean from this class ... Something like that:

      public:
      CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > GetBitmapArray(){return m_arrBitmapData;}

      because this trial give me:

      error C2558: class 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : no copy constructor available

      F 1 Reply Last reply
      0
      • _ _Flaviu

        Ok, resize issue has been solved. Secondly, as long m_arrBitmapData is private memeber, I want to return this memeber through a public method, I mean from this class ... Something like that:

        public:
        CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > GetBitmapArray(){return m_arrBitmapData;}

        because this trial give me:

        error C2558: class 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : no copy constructor available

        F Offline
        F Offline
        Freak30
        wrote on last edited by
        #5

        Just return a constant reference as in

        const CArray< CArray, CArray > & GetBitmapArray()

        The good thing about pessimism is, that you are always either right or pleasently surprised.

        _ 1 Reply Last reply
        0
        • F Freak30

          Just return a constant reference as in

          const CArray< CArray, CArray > & GetBitmapArray()

          The good thing about pessimism is, that you are always either right or pleasently surprised.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #6

          I think is working ... :) I have one more question: there is a way to pass a paramanter as CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> >, just like:

          // header
          void SetPlotData(CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > InputArray);

          and

          // implementation
          void CGraphCtrl::SetPlotData(CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > InputArray)
          {
          if ((m_PixelNumberX>0) && (m_PixelNumberY > 0))
          {
          PlotData = InputArray;
          }
          }

          because code above doesn't work:

          error C2582: 'CArray<class CArray<unsigned long,unsigned long &>,class CArray<unsigned long,unsigned long &> >' : 'operator =' function is unavailable

          As soon as I solve this error, my program will run ...

          1 Reply Last reply
          0
          • _ _Flaviu

            I have a class (taken from somewhere). Here, I have a member like this:

            public:
            void SetBitmapDataSize(const int x, const int y);

            private:
            int m_PixelNumberX;
            int m_PixelNumberY;
            vector > BitmapData;

            and SetBitmapDataSize is implemented:

            void CBitmapOp::SetBitmapDataSize(const int x, const int y)
            {
            m_PixelNumberX = x;
            m_PixelNumberY = y;
            if((x>=0) && (y>=0))
            {
            BitmapData.resize(m_PixelNumberX);
            for(int x = 0; x < m_PixelNumberX; x++)
            BitmapData[x].resize(m_PixelNumberY);
            }
            }

            Now, I want to replace vector > with this CArray: I had tried:

            private:
            int m_PixelNumberX;
            int m_PixelNumberY;
            CArray< CArray, CArray > m_arrBitmapData;

            my question is, how can replace vector.resize method, and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ? Thank you.

            N Offline
            N Offline
            nv3
            wrote on last edited by
            #7

            I would neither store nor return the bitmap data in that way. What you are creating here is a so-called "ragged" array, in which each row can have a different length. In a bitmap this luxury is not needed, all rows are of equal length by definition. Therefore a one-dimensional array is good enough and in some regards even superior. a) A one-dimensional array can be allocated in one chunk (whereas your ragged array needs N allocations, N being the number of rows) b) A one-dimensional array does not incur the overhead that comes with a ragged array, which is easily 3 x 64bits per row. To access the members of the one-dimensional array in a 2D fashion, simply use the algorithm:

            index of pixel (x, y) = x + y\*pixelsPerRow
            

            As a return value for your access function I would suggest you use a simple pointer (const COLORREF*). For the caller to know how to access that 1D array you should provide addition access functions for the number or rows and number of columns.

            _ 1 Reply Last reply
            0
            • N nv3

              I would neither store nor return the bitmap data in that way. What you are creating here is a so-called "ragged" array, in which each row can have a different length. In a bitmap this luxury is not needed, all rows are of equal length by definition. Therefore a one-dimensional array is good enough and in some regards even superior. a) A one-dimensional array can be allocated in one chunk (whereas your ragged array needs N allocations, N being the number of rows) b) A one-dimensional array does not incur the overhead that comes with a ragged array, which is easily 3 x 64bits per row. To access the members of the one-dimensional array in a 2D fashion, simply use the algorithm:

              index of pixel (x, y) = x + y\*pixelsPerRow
              

              As a return value for your access function I would suggest you use a simple pointer (const COLORREF*). For the caller to know how to access that 1D array you should provide addition access functions for the number or rows and number of columns.

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #8

              Ohh, I would love to get rif of 2D array ... I had taken the class from here[^], could you assit me how to give up vector> BitmapData ? If I am not asking too much ... I will start working, to see what is happen ... See you.

              N 1 Reply Last reply
              0
              • _ _Flaviu

                Ohh, I would love to get rif of 2D array ... I had taken the class from here[^], could you assit me how to give up vector> BitmapData ? If I am not asking too much ... I will start working, to see what is happen ... See you.

                N Offline
                N Offline
                nv3
                wrote on last edited by
                #9

                It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.

                _ 2 Replies Last reply
                0
                • N nv3

                  It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.

                  _ Offline
                  _ Offline
                  _Flaviu
                  wrote on last edited by
                  #10

                  Kindly thank you NV3 ! I will let you know what I have done. :)

                  1 Reply Last reply
                  0
                  • N nv3

                    It is not very difficult to get rid of this 2D array. 1) replace it with a vector m_bitmapData; 2) Replace any reference to the old BitmapData array as follows: BitmapData[x][y] should be replaced by m_bitmapData[x + m_PixelNumberX*y] 3) In SetBitmapDataSize allocate the new array by m_bitmapData.resize (x * y); Change the function interfaces accordingly. Should be really easy.

                    _ Offline
                    _ Offline
                    _Flaviu
                    wrote on last edited by
                    #11

                    Great ! Is working, just like the original one ... I didn't replace 2D array by vector, but with CArray ... and is working ! Kindly thank you, NV3 !

                    N 1 Reply Last reply
                    0
                    • _ _Flaviu

                      Great ! Is working, just like the original one ... I didn't replace 2D array by vector, but with CArray ... and is working ! Kindly thank you, NV3 !

                      N Offline
                      N Offline
                      nv3
                      wrote on last edited by
                      #12

                      You are welcome.

                      1 Reply Last reply
                      0
                      • _ _Flaviu

                        I have a class (taken from somewhere). Here, I have a member like this:

                        public:
                        void SetBitmapDataSize(const int x, const int y);

                        private:
                        int m_PixelNumberX;
                        int m_PixelNumberY;
                        vector > BitmapData;

                        and SetBitmapDataSize is implemented:

                        void CBitmapOp::SetBitmapDataSize(const int x, const int y)
                        {
                        m_PixelNumberX = x;
                        m_PixelNumberY = y;
                        if((x>=0) && (y>=0))
                        {
                        BitmapData.resize(m_PixelNumberX);
                        for(int x = 0; x < m_PixelNumberX; x++)
                        BitmapData[x].resize(m_PixelNumberY);
                        }
                        }

                        Now, I want to replace vector > with this CArray: I had tried:

                        private:
                        int m_PixelNumberX;
                        int m_PixelNumberY;
                        CArray< CArray, CArray > m_arrBitmapData;

                        my question is, how can replace vector.resize method, and how can I return CArray< CArray<COLORREF, COLORREF&>, CArray<COLORREF, COLORREF&> > data type ? Thank you.

                        R Offline
                        R Offline
                        Raushank03
                        wrote on last edited by
                        #13

                        Your answer is available on http://techgurulab.com/course/c-study-material/

                        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